Skip to content

Commit 73132ba

Browse files
author
Steve Canny
committed
img: add width, height, and content_type properties
1 parent e4934e8 commit 73132ba

4 files changed

Lines changed: 35 additions & 3 deletions

File tree

docx/image/constants.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,17 @@
55
"""
66

77

8+
class MIME_TYPE(object):
9+
"""
10+
Image content types.
11+
"""
12+
PNG = 'image/png'
13+
14+
815
class TAG(object):
16+
"""
17+
Identifiers for image attribute tags.
18+
"""
919

1020
PX_WIDTH = 'px_width'
1121
PX_HEIGHT = 'px_height'

docx/image/image.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ def from_file(cls, image_descriptor):
4444
filename = None
4545
return cls._from_stream(stream, blob, filename)
4646

47+
@property
48+
def px_width(self):
49+
"""
50+
The horizontal pixel dimension of the image
51+
"""
52+
return self._px_width
53+
54+
@property
55+
def px_height(self):
56+
"""
57+
The vertical pixel dimension of the image
58+
"""
59+
return self._px_height
60+
4761
@classmethod
4862
def _from_stream(cls, stream, blob, filename=None):
4963
"""

docx/image/png.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import absolute_import, division, print_function
44

5-
from .constants import TAG
5+
from .constants import MIME_TYPE, TAG
66
from .exceptions import InvalidImageStreamError
77
from .helpers import StreamReader
88
from .image import Image
@@ -20,6 +20,14 @@ class Png(Image):
2020
def __init__(self, blob, filename, cx, cy, attrs):
2121
super(Png, self).__init__(blob, filename, cx, cy, attrs)
2222

23+
@property
24+
def content_type(self):
25+
"""
26+
MIME content type for this image, unconditionally `image/png` for
27+
PNG images.
28+
"""
29+
return MIME_TYPE.PNG
30+
2331
@classmethod
2432
def from_stream(cls, stream, blob, filename):
2533
"""

features/img-characterize-image.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@ Feature: Characterize an image file
1414
And the image has <vert_dpi> vertical dpi
1515

1616
Examples: Image file characteristics
17-
| filename | mime_type | cx | cy | horz_dpi | vert_dpi |
18-
| test.png | image/png | 11 | 22 | 333 | 444 |
17+
| filename | mime_type | cx | cy | horz_dpi | vert_dpi |
18+
| test.png | image/png | 901 | 1350 | 333 | 444 |

0 commit comments

Comments
 (0)