Skip to content

Commit

Permalink
img: #124 default to inches on no TIFF res unit
Browse files Browse the repository at this point in the history
  • Loading branch information
Colin McDonald authored and Steve Canny committed Feb 20, 2015
1 parent 1a7358e commit ffdb139
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
Release History
---------------

NEXT
++++++++++++++++++

- Fix #124: default to inches on no TIFF resolution unit


0.8.3 (2015-02-19)
++++++++++++++++++

Expand Down
14 changes: 11 additions & 3 deletions docx/image/tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,22 @@ def _dpi(self, resolution_tag):
calculation is based on the values of both that tag and the
TIFF_TAG.RESOLUTION_UNIT tag in this parser's |_IfdEntries| instance.
"""
if resolution_tag not in self._ifd_entries:
ifd_entries = self._ifd_entries

if resolution_tag not in ifd_entries:
return 72
resolution_unit = self._ifd_entries[TIFF_TAG.RESOLUTION_UNIT]

# resolution unit defaults to inches (2)
resolution_unit = (
ifd_entries[TIFF_TAG.RESOLUTION_UNIT]
if TIFF_TAG.RESOLUTION_UNIT in ifd_entries else 2
)

if resolution_unit == 1: # aspect ratio only
return 72
# resolution_unit == 2 for inches, 3 for centimeters
units_per_inch = 1 if resolution_unit == 2 else 2.54
dots_per_unit = self._ifd_entries[resolution_tag]
dots_per_unit = ifd_entries[resolution_tag]
return int(round(dots_per_unit * units_per_inch))

@classmethod
Expand Down
13 changes: 8 additions & 5 deletions tests/image/test_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,19 @@ def it_knows_the_horz_and_vert_dpi_after_parsing(self, dpi_fixture):
# fixtures -------------------------------------------------------

@pytest.fixture(params=[
(1, 150, 240, 72, 72),
(2, 42, 24, 42, 24),
(3, 100, 200, 254, 508),
(6, None, None, 72, 72),
(1, 150, 240, 72, 72),
(2, 42, 24, 42, 24),
(3, 100, 200, 254, 508),
(2, None, None, 72, 72),
(None, 96, 100, 96, 100),
])
def dpi_fixture(self, request):
resolution_unit, x_resolution, y_resolution = request.param[:3]
expected_horz_dpi, expected_vert_dpi = request.param[3:]

entries = {TIFF_TAG.RESOLUTION_UNIT: resolution_unit}
entries = {}
if resolution_unit is not None:
entries[TIFF_TAG.RESOLUTION_UNIT] = resolution_unit
if x_resolution is not None:
entries[TIFF_TAG.X_RESOLUTION] = x_resolution
if y_resolution is not None:
Expand Down

0 comments on commit ffdb139

Please sign in to comment.