Skip to content

Commit 47a72f3

Browse files
author
Steve Canny
committed
img: add Jpeg.content_type
1 parent da83e15 commit 47a72f3

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

docx/image/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ class MIME_TYPE(object):
9797
"""
9898
Image content types.
9999
"""
100+
JPEG = 'image/jpeg'
100101
PNG = 'image/png'
101102

102103

docx/image/jpeg.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from __future__ import absolute_import, division, print_function
99

10-
from .constants import JPEG_MARKER_CODE
10+
from .constants import JPEG_MARKER_CODE, MIME_TYPE
1111
from .helpers import BIG_ENDIAN, StreamReader
1212
from .image import Image
1313

@@ -16,6 +16,13 @@ class Jpeg(Image):
1616
"""
1717
Base class for JFIF and EXIF subclasses.
1818
"""
19+
@property
20+
def content_type(self):
21+
"""
22+
MIME content type for this image, unconditionally `image/jpeg` for
23+
JPEG images.
24+
"""
25+
return MIME_TYPE.JPEG
1926

2027

2128
class Exif(Jpeg):

features/img-characterize-image.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ Feature: Characterize an image file
33
As a programmer using the advanced python-docx API
44
I need a way to determine the image content type and size
55

6-
@wip
76
Scenario Outline: Characterize an image file
87
Given the image file '<filename>'
98
When I construct an image using the image path
@@ -18,3 +17,4 @@ Feature: Characterize an image file
1817
| test.png | image/png | 901 | 1350 | 150 | 150 |
1918
| monty-truth.png | image/png | 150 | 214 | 72 | 72 |
2019
| jfif-300-dpi.jpg | image/jpeg | 1504 | 1936 | 300 | 300 |
20+
| lena_std.jpg | image/jpeg | 512 | 512 | 72 | 72 |
102 KB
Loading

tests/image/test_jpeg.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,23 @@
1111
from mock import call
1212

1313
from docx.compat import BytesIO
14-
from docx.image.constants import JPEG_MARKER_CODE
14+
from docx.image.constants import JPEG_MARKER_CODE, MIME_TYPE
1515
from docx.image.helpers import BIG_ENDIAN, StreamReader
1616
from docx.image.jpeg import (
17-
_App0Marker, Jfif, _JfifMarkers, _Marker, _MarkerFactory, _MarkerFinder,
18-
_MarkerParser, _SofMarker
17+
_App0Marker, Jfif, _JfifMarkers, Jpeg, _Marker, _MarkerFactory,
18+
_MarkerFinder, _MarkerParser, _SofMarker
1919
)
2020

2121
from ..unitutil import class_mock, initializer_mock, instance_mock
2222

2323

24+
class DescribeJpeg(object):
25+
26+
def it_knows_its_content_type(self):
27+
jpeg = Jpeg(None, None, None, None, None)
28+
assert jpeg.content_type == MIME_TYPE.JPEG
29+
30+
2431
class DescribeJfif(object):
2532

2633
def it_can_construct_from_a_jfif_stream(self, from_stream_fixture):

0 commit comments

Comments
 (0)