MCPcopy Index your code
hub / github.com/python-openxml/python-docx / ImagePart

Class ImagePart

src/docx/parts/image.py:17–80  ·  view source on GitHub ↗

An image part. Corresponds to the target part of a relationship with type RELATIONSHIP_TYPE.IMAGE.

Source from the content-addressed store, hash-verified

15
16
17class ImagePart(Part):
18 """An image part.
19
20 Corresponds to the target part of a relationship with type RELATIONSHIP_TYPE.IMAGE.
21 """
22
23 def __init__(
24 self, partname: PackURI, content_type: str, blob: bytes, image: Image | None = None
25 ):
26 super(ImagePart, self).__init__(partname, content_type, blob)
27 self._image = image
28
29 @property
30 def default_cx(self):
31 """Native width of this image, calculated from its width in pixels and
32 horizontal dots per inch (dpi)."""
33 px_width = self.image.px_width
34 horz_dpi = self.image.horz_dpi
35 width_in_inches = px_width / horz_dpi
36 return Inches(width_in_inches)
37
38 @property
39 def default_cy(self):
40 """Native height of this image, calculated from its height in pixels and
41 vertical dots per inch (dpi)."""
42 px_height = self.image.px_height
43 horz_dpi = self.image.horz_dpi
44 height_in_emu = int(round(914400 * px_height / horz_dpi))
45 return Emu(height_in_emu)
46
47 @property
48 def filename(self):
49 """Filename from which this image part was originally created.
50
51 A generic name, e.g. 'image.png', is substituted if no name is available, for
52 example when the image was loaded from an unnamed stream. In that case a default
53 extension is applied based on the detected MIME type of the image.
54 """
55 if self._image is not None:
56 return self._image.filename
57 return "image.%s" % self.partname.ext
58
59 @classmethod
60 def from_image(cls, image: Image, partname: PackURI):
61 """Return an |ImagePart| instance newly created from `image` and assigned
62 `partname`."""
63 return ImagePart(partname, image.content_type, image.blob, image)
64
65 @property
66 def image(self) -> Image:
67 if self._image is None:
68 self._image = Image.from_blob(self.blob)
69 return self._image
70
71 @classmethod
72 def load(cls, partname: PackURI, content_type: str, blob: bytes, package: OpcPackage):
73 """Called by ``docx.opc.package.PartFactory`` to load an image part from a
74 package being opened by ``Document(...)`` call."""

Callers 3

filename_fixtureMethod · 0.90
from_imageMethod · 0.85

Calls

no outgoing calls

Tested by 2

filename_fixtureMethod · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…