44The proxy class for an image part, and related objects.
55"""
66
7- from __future__ import absolute_import , print_function , unicode_literals
7+ from __future__ import (
8+ absolute_import , division , print_function , unicode_literals
9+ )
810
911import hashlib
1012import os
1618
1719from docx .opc .constants import CONTENT_TYPE as CT
1820from docx .opc .package import Part
19- from docx .shared import lazyproperty
21+ from docx .shared import Emu , Inches , lazyproperty
2022
2123
2224class Image (object ):
@@ -200,6 +202,28 @@ def __init__(self, partname, content_type, blob, image=None):
200202 super (ImagePart , self ).__init__ (partname , content_type , blob )
201203 self ._image = image
202204
205+ @property
206+ def default_cx (self ):
207+ """
208+ Native width of this image, calculated from its width in pixels and
209+ horizontal dots per inch (dpi).
210+ """
211+ px_width = self ._image .px_width
212+ horz_dpi = self ._image .horz_dpi
213+ width_in_inches = px_width / horz_dpi
214+ return Inches (width_in_inches )
215+
216+ @property
217+ def default_cy (self ):
218+ """
219+ Native height of this image, calculated from its height in pixels and
220+ vertical dots per inch (dpi).
221+ """
222+ px_height = self ._image .px_height
223+ horz_dpi = self ._image .horz_dpi
224+ height_in_emu = 914400 * px_height / horz_dpi
225+ return Emu (height_in_emu )
226+
203227 @property
204228 def filename (self ):
205229 """
@@ -219,16 +243,6 @@ def from_image(cls, image, partname):
219243 """
220244 return ImagePart (partname , image .content_type , image .blob , image )
221245
222- @property
223- def height (self ):
224- """
225- Native height of this image, calculated from its height in pixels and
226- vertical dots per inch (dpi) when available. Default values are
227- silently substituted when specific values cannot be parsed from the
228- binary image byte stream.
229- """
230- raise NotImplementedError
231-
232246 @classmethod
233247 def load (cls , partname , content_type , blob , package ):
234248 """
@@ -244,13 +258,3 @@ def sha1(self):
244258 SHA1 hash digest of the blob of this image part.
245259 """
246260 raise NotImplementedError
247-
248- @property
249- def width (self ):
250- """
251- Native width of this image, calculated from its width in pixels and
252- horizontal dots per inch (dpi) when available. Default values are
253- silently substituted when specific values cannot be parsed from the
254- binary image byte stream.
255- """
256- raise NotImplementedError
0 commit comments