Skip to content

Commit dd56159

Browse files
author
Steve Canny
committed
img: add Png.vert_dpi
1 parent 17866e3 commit dd56159

3 files changed

Lines changed: 38 additions & 22 deletions

File tree

docx/image/png.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,27 @@ def horz_dpi(self):
4747
"""
4848
units_specifier = self._attrs.get(TAG.UNITS_SPECIFIER)
4949
horz_px_per_unit = self._attrs.get(TAG.HORZ_PX_PER_UNIT)
50+
return self._dpi(units_specifier, horz_px_per_unit)
5051

51-
if units_specifier == 1 and horz_px_per_unit is not None:
52-
horz_dpi = int(round(horz_px_per_unit * 0.0254))
53-
else:
54-
horz_dpi = 72
52+
@property
53+
def vert_dpi(self):
54+
"""
55+
Integer dots per inch for the height of this image. Defaults to 72
56+
when not present in the file, as is often the case.
57+
"""
58+
units_specifier = self._attrs.get(TAG.UNITS_SPECIFIER)
59+
vert_px_per_unit = self._attrs.get(TAG.VERT_PX_PER_UNIT)
60+
return self._dpi(units_specifier, vert_px_per_unit)
5561

56-
return horz_dpi
62+
@staticmethod
63+
def _dpi(units_specifier, px_per_unit):
64+
"""
65+
Return dots per inch value calculated from *units_specifier* and
66+
*px_per_unit*.
67+
"""
68+
if units_specifier == 1 and px_per_unit is not None:
69+
return int(round(px_per_unit * 0.0254))
70+
return 72
5771

5872
@classmethod
5973
def _parse_png_headers(cls, stream):

features/img-characterize-image.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ Feature: Characterize an image file
1515

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

tests/image/test_png.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ def it_can_parse_an_pHYs_chunk(self, parse_pHYs_fixture):
7171
attrs = Png._parse_pHYs(stream, offset)
7272
assert attrs == expected_attrs
7373

74-
def it_knows_its_horizontal_dpi(self, horz_dpi_fixture):
75-
png, expected_horz_dpi = horz_dpi_fixture
76-
assert png.horz_dpi == expected_horz_dpi
74+
def it_knows_its_dpi(self, dpi_fixture):
75+
png, expected_dpi = dpi_fixture
76+
assert png.horz_dpi == expected_dpi
77+
assert png.vert_dpi == expected_dpi
7778

7879
# fixtures -------------------------------------------------------
7980

@@ -109,6 +110,20 @@ def chunk_offset_fixture(self, request):
109110
def chunk_offsets(self, request):
110111
return dict()
111112

113+
@pytest.fixture(params=[
114+
(5906, 1, 150), (11811, 1, 300), (5906, 0, 72), (None, 0, 72),
115+
(666, 0, 72), (2835, 1, 72)
116+
])
117+
def dpi_fixture(self, request):
118+
px_per_unit, units_specifier, expected_dpi = request.param
119+
attrs = {
120+
TAG.HORZ_PX_PER_UNIT: px_per_unit,
121+
TAG.VERT_PX_PER_UNIT: px_per_unit,
122+
TAG.UNITS_SPECIFIER: units_specifier
123+
}
124+
png = Png(None, None, None, None, attrs)
125+
return png, expected_dpi
126+
112127
@pytest.fixture
113128
def filename_(self, request):
114129
return instance_mock(request, str)
@@ -124,19 +139,6 @@ def from_stream_fixture(
124139
stream_rdr_, Png__init__, cx, cy, attrs, png_
125140
)
126141

127-
@pytest.fixture(params=[
128-
(5906, 1, 150), (11811, 1, 300), (5906, 0, 72), (None, 0, 72),
129-
(666, 0, 72), (2835, 1, 72)
130-
])
131-
def horz_dpi_fixture(self, request):
132-
horz_px_per_unit, units_specifier, expected_horz_dpi = request.param
133-
attrs = {
134-
TAG.HORZ_PX_PER_UNIT: horz_px_per_unit,
135-
TAG.UNITS_SPECIFIER: units_specifier
136-
}
137-
png = Png(None, None, None, None, attrs)
138-
return png, expected_horz_dpi
139-
140142
@pytest.fixture
141143
def no_IHDR_fixture(self, stream_, chunk_offsets):
142144
return stream_, chunk_offsets

0 commit comments

Comments
 (0)