Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 70e0948

Browse files
author
Steve Canny
committed
img: add _PngParser.px_width and .px_height
1 parent 635131b commit 70e0948

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

docx/image/png.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ class _PngParser(object):
167167
Parses a PNG image stream to extract the image properties found in its
168168
chunks.
169169
"""
170+
def __init__(self, chunks):
171+
super(_PngParser, self).__init__()
172+
self._chunks = chunks
173+
170174
@classmethod
171175
def parse(cls, stream):
172176
"""
@@ -181,14 +185,16 @@ def px_width(self):
181185
"""
182186
The number of pixels in each row of the image.
183187
"""
184-
raise NotImplementedError
188+
IHDR = self._chunks.IHDR
189+
return IHDR.px_width
185190

186191
@property
187192
def px_height(self):
188193
"""
189194
The number of stacked rows of pixels in the image.
190195
"""
191-
raise NotImplementedError
196+
IHDR = self._chunks.IHDR
197+
return IHDR.px_height
192198

193199
@property
194200
def horz_dpi(self):
@@ -211,6 +217,10 @@ class _Chunks(object):
211217
"""
212218
Collection of the chunks parsed from a PNG image stream
213219
"""
220+
def __init__(self, chunk_lst):
221+
super(_Chunks, self).__init__()
222+
self._chunks = chunk_lst
223+
214224
@classmethod
215225
def from_stream(cls, stream):
216226
"""
@@ -220,6 +230,13 @@ def from_stream(cls, stream):
220230
chunk_lst = [chunk for chunk in chunk_parser.iter_chunks()]
221231
return cls(chunk_lst)
222232

233+
@property
234+
def IHDR(self):
235+
"""
236+
IHDR chunk in PNG image
237+
"""
238+
raise NotImplementedError
239+
223240

224241
class _ChunkParser(object):
225242
"""

tests/image/test_png.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ def it_can_parse_the_headers_of_a_PNG_stream(self, parse_fixture):
266266
_PngParser__init_.assert_called_once_with(chunks_)
267267
assert isinstance(png_parser, _PngParser)
268268

269+
def it_knows_the_image_width_and_height(self, dimensions_fixture):
270+
png_parser, px_width, px_height = dimensions_fixture
271+
assert png_parser.px_width == px_width
272+
assert png_parser.px_height == px_height
273+
269274
# fixtures -------------------------------------------------------
270275

271276
@pytest.fixture
@@ -278,6 +283,14 @@ def _Chunks_(self, request, chunks_):
278283
def chunks_(self, request):
279284
return instance_mock(request, _Chunks)
280285

286+
@pytest.fixture
287+
def dimensions_fixture(self, chunks_):
288+
px_width, px_height = 12, 34
289+
chunks_.IHDR.px_width = px_width
290+
chunks_.IHDR.px_height = px_height
291+
png_parser = _PngParser(chunks_)
292+
return png_parser, px_width, px_height
293+
281294
@pytest.fixture
282295
def parse_fixture(self, stream_, _Chunks_, _PngParser__init_, chunks_):
283296
return stream_, _Chunks_, _PngParser__init_, chunks_

0 commit comments

Comments
 (0)