Skip to content

Commit cdb462d

Browse files
author
Steve Canny
committed
img: add _TiffParser.px_width and .px_height
1 parent b31b011 commit cdb462d

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

docx/image/constants.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,40 @@ class TIFF_FLD_TYPE(object):
130130

131131

132132
TIFF_FLD = TIFF_FLD_TYPE
133+
134+
135+
class TIFF_TAG(object):
136+
"""
137+
Tag codes for TIFF Image File Directory (IFD) entries.
138+
"""
139+
IMAGE_WIDTH = 0x0100
140+
IMAGE_LENGTH = 0x0101
141+
X_RESOLUTION = 0x011A
142+
Y_RESOLUTION = 0x011B
143+
RESOLUTION_UNIT = 0x0128
144+
145+
tag_names = {
146+
0x00FE: 'NewSubfileType',
147+
0x0100: 'ImageWidth',
148+
0x0101: 'ImageLength',
149+
0x0102: 'BitsPerSample',
150+
0x0103: 'Compression',
151+
0x0106: 'PhotometricInterpretation',
152+
0x010E: 'ImageDescription',
153+
0x010F: 'Make',
154+
0x0110: 'Model',
155+
0x0111: 'StripOffsets',
156+
0x0112: 'Orientation',
157+
0x0115: 'SamplesPerPixel',
158+
0x0117: 'StripByteCounts',
159+
0x011A: 'XResolution',
160+
0x011B: 'YResolution',
161+
0x011C: 'PlanarConfiguration',
162+
0x0128: 'ResolutionUnit',
163+
0x0131: 'Software',
164+
0x0132: 'DateTime',
165+
0x0213: 'YCbCrPositioning',
166+
0x8769: 'ExifTag',
167+
0x8825: 'GPS IFD',
168+
0xC4A5: 'PrintImageMatching',
169+
}

docx/image/tiff.py

Lines changed: 16 additions & 5 deletions
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 TIFF_FLD
5+
from .constants import TIFF_FLD, TIFF_TAG
66
from .helpers import BIG_ENDIAN, LITTLE_ENDIAN, StreamReader
77
from .image import Image
88

@@ -31,9 +31,9 @@ class _TiffParser(object):
3131
Parses a TIFF image stream to extract the image properties found in its
3232
main image file directory (IFD)
3333
"""
34-
def __init__(self, ifd):
34+
def __init__(self, ifd_entries):
3535
super(_TiffParser, self).__init__()
36-
self._ifd = ifd
36+
self._ifd_entries = ifd_entries
3737

3838
@classmethod
3939
def parse(cls, stream):
@@ -62,7 +62,7 @@ def px_height(self):
6262
contains no ``ImageLength`` tag, the expected case when the TIFF is
6363
embeded in an Exif image.
6464
"""
65-
raise NotImplementedError
65+
return self._ifd_entries.get(TIFF_TAG.IMAGE_LENGTH)
6666

6767
@property
6868
def px_width(self):
@@ -71,7 +71,7 @@ def px_width(self):
7171
contains no ``ImageWidth`` tag, the expected case when the TIFF is
7272
embeded in an Exif image.
7373
"""
74-
raise NotImplementedError
74+
return self._ifd_entries.get(TIFF_TAG.IMAGE_WIDTH)
7575

7676
@property
7777
def vert_dpi(self):
@@ -108,6 +108,10 @@ class _IfdEntries(object):
108108
Image File Directory for a TIFF image, having mapping (dict) semantics
109109
allowing "tag" values to be retrieved by tag code.
110110
"""
111+
def __init__(self, entries):
112+
super(_IfdEntries, self).__init__()
113+
self._entries = entries
114+
111115
@classmethod
112116
def from_stream(cls, stream, offset):
113117
"""
@@ -118,6 +122,13 @@ def from_stream(cls, stream, offset):
118122
entries = dict((e.tag, e.value) for e in ifd_parser.iter_entries())
119123
return cls(entries)
120124

125+
def get(self, tag_code, default=None):
126+
"""
127+
Return value of IFD entry having tag matching *tag_code*, or
128+
*default* if no matching tag found.
129+
"""
130+
return self._entries.get(tag_code, default)
131+
121132

122133
class _IfdParser(object):
123134
"""

tests/image/test_tiff.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from mock import call
1212

1313
from docx.compat import BytesIO
14+
from docx.image.constants import TIFF_TAG
1415
from docx.image.helpers import BIG_ENDIAN, LITTLE_ENDIAN, StreamReader
1516
from docx.image.tiff import (
1617
_IfdEntries, _IfdEntry, _IfdEntryFactory, _IfdParser, Tiff, _TiffParser
@@ -98,6 +99,17 @@ def it_makes_a_stream_reader_to_help_parse(self, mk_stream_rdr_fixture):
9899
StreamReader_.assert_called_once_with(stream, endian)
99100
assert stream_rdr is stream_rdr_
100101

102+
def it_knows_image_width_and_height_after_parsing(self):
103+
px_width, px_height = 42, 24
104+
entries = {
105+
TIFF_TAG.IMAGE_WIDTH: px_width,
106+
TIFF_TAG.IMAGE_LENGTH: px_height,
107+
}
108+
ifd_entries = _IfdEntries(entries)
109+
tiff_parser = _TiffParser(ifd_entries)
110+
assert tiff_parser.px_width == px_width
111+
assert tiff_parser.px_height == px_height
112+
101113
# fixtures -------------------------------------------------------
102114

103115
@pytest.fixture

0 commit comments

Comments
 (0)