Skip to content

Commit 66fedd4

Browse files
author
Steve Canny
committed
img: add Image.width and .height
1 parent 0d63564 commit 66fedd4

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

docx/image/image.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
import os
1212

1313
from ..compat import BytesIO, is_string
14-
from ..shared import lazyproperty
1514
from .exceptions import UnrecognizedImageError
15+
from ..shared import Inches, lazyproperty
1616

1717

1818
class Image(object):
@@ -117,6 +117,22 @@ def vert_dpi(self):
117117
"""
118118
return self._image_header.vert_dpi
119119

120+
@property
121+
def width(self):
122+
"""
123+
A |Length| value representing the native width of the image,
124+
calculated from the values of `px_width` and `horz_dpi`.
125+
"""
126+
return Inches(self.px_width / self.horz_dpi)
127+
128+
@property
129+
def height(self):
130+
"""
131+
A |Length| value representing the native height of the image,
132+
calculated from the values of `px_height` and `vert_dpi`.
133+
"""
134+
return Inches(self.px_height / self.vert_dpi)
135+
120136
def scaled_dimensions(self, width, height):
121137
"""
122138
Return a (cx, cy) 2-tuple representing the native dimensions of this

tests/image/test_image.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from docx.image.png import Png
1818
from docx.image.tiff import Tiff
1919
from docx.opc.constants import CONTENT_TYPE as CT
20+
from docx.shared import Length
2021

2122
from ..unitutil.file import test_file
2223
from ..unitutil.mock import (
@@ -71,7 +72,7 @@ def it_knows_the_image_content_type(self, content_type_fixture):
7172
image = Image(None, None, image_header_)
7273
assert image.content_type == content_type
7374

74-
def it_knows_the_image_dimensions(self, dimensions_fixture):
75+
def it_knows_the_image_px_dimensions(self, dimensions_fixture):
7576
image_header_, px_width, px_height = dimensions_fixture
7677
image = Image(None, None, image_header_)
7778
assert image.px_width == px_width
@@ -83,6 +84,12 @@ def it_knows_the_horz_and_vert_dpi_of_the_image(self, dpi_fixture):
8384
assert image.horz_dpi == horz_dpi
8485
assert image.vert_dpi == vert_dpi
8586

87+
def it_knows_the_image_native_size(self, size_fixture):
88+
image, width, height = size_fixture
89+
assert (image.width, image.height) == (width, height)
90+
assert isinstance(image.width, Length)
91+
assert isinstance(image.height, Length)
92+
8693
def it_knows_the_image_filename(self):
8794
filename = 'foobar.png'
8895
image = Image(None, filename, None)
@@ -182,6 +189,13 @@ def known_image_fixture(self, request):
182189
image_filename, characteristics = cases[request.param]
183190
return image_filename, characteristics
184191

192+
@pytest.fixture
193+
def size_fixture(self, image_header_):
194+
image_header_.px_width, image_header_.px_height = 150, 75
195+
image_header_.horz_dpi, image_header_.vert_dpi = 72, 200
196+
image = Image(None, None, image_header_)
197+
return image, 1905000, 342900
198+
185199
# fixture components ---------------------------------------------
186200

187201
@pytest.fixture

0 commit comments

Comments
 (0)