Skip to content

Commit ffdb139

Browse files
Colin McDonaldSteve Canny
authored andcommitted
img: python-openxml#124 default to inches on no TIFF res unit
1 parent 1a7358e commit ffdb139

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

HISTORY.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Release History
44
---------------
55

6+
NEXT
7+
++++++++++++++++++
8+
9+
- Fix #124: default to inches on no TIFF resolution unit
10+
11+
612
0.8.3 (2015-02-19)
713
++++++++++++++++++
814

docx/image/tiff.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,22 @@ def _dpi(self, resolution_tag):
116116
calculation is based on the values of both that tag and the
117117
TIFF_TAG.RESOLUTION_UNIT tag in this parser's |_IfdEntries| instance.
118118
"""
119-
if resolution_tag not in self._ifd_entries:
119+
ifd_entries = self._ifd_entries
120+
121+
if resolution_tag not in ifd_entries:
120122
return 72
121-
resolution_unit = self._ifd_entries[TIFF_TAG.RESOLUTION_UNIT]
123+
124+
# resolution unit defaults to inches (2)
125+
resolution_unit = (
126+
ifd_entries[TIFF_TAG.RESOLUTION_UNIT]
127+
if TIFF_TAG.RESOLUTION_UNIT in ifd_entries else 2
128+
)
129+
122130
if resolution_unit == 1: # aspect ratio only
123131
return 72
124132
# resolution_unit == 2 for inches, 3 for centimeters
125133
units_per_inch = 1 if resolution_unit == 2 else 2.54
126-
dots_per_unit = self._ifd_entries[resolution_tag]
134+
dots_per_unit = ifd_entries[resolution_tag]
127135
return int(round(dots_per_unit * units_per_inch))
128136

129137
@classmethod

tests/image/test_tiff.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,19 @@ def it_knows_the_horz_and_vert_dpi_after_parsing(self, dpi_fixture):
116116
# fixtures -------------------------------------------------------
117117

118118
@pytest.fixture(params=[
119-
(1, 150, 240, 72, 72),
120-
(2, 42, 24, 42, 24),
121-
(3, 100, 200, 254, 508),
122-
(6, None, None, 72, 72),
119+
(1, 150, 240, 72, 72),
120+
(2, 42, 24, 42, 24),
121+
(3, 100, 200, 254, 508),
122+
(2, None, None, 72, 72),
123+
(None, 96, 100, 96, 100),
123124
])
124125
def dpi_fixture(self, request):
125126
resolution_unit, x_resolution, y_resolution = request.param[:3]
126127
expected_horz_dpi, expected_vert_dpi = request.param[3:]
127128

128-
entries = {TIFF_TAG.RESOLUTION_UNIT: resolution_unit}
129+
entries = {}
130+
if resolution_unit is not None:
131+
entries[TIFF_TAG.RESOLUTION_UNIT] = resolution_unit
129132
if x_resolution is not None:
130133
entries[TIFF_TAG.X_RESOLUTION] = x_resolution
131134
if y_resolution is not None:

0 commit comments

Comments
 (0)