Skip to content

image

image

The proxy class for an image part, and related objects.

ImagePart

ImagePart(
    partname: PackURI,
    content_type: str,
    blob: bytes,
    image: Image | None = None,
)

Bases: Part

An image part.

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

Source code in src/docx/parts/image.py
def __init__(
    self, partname: PackURI, content_type: str, blob: bytes, image: Image | None = None
):
    super(ImagePart, self).__init__(partname, content_type, blob)
    self._image = image

default_cx property

default_cx

Native width of this image, calculated from its width in pixels and horizontal dots per inch (dpi).

default_cy property

default_cy

Native height of this image, calculated from its height in pixels and vertical dots per inch (dpi).

filename property

filename

Filename from which this image part was originally created.

A generic name, e.g. 'image.png', is substituted if no name is available, for example when the image was loaded from an unnamed stream. In that case a default extension is applied based on the detected MIME type of the image.

sha1 property

sha1

SHA1 hash digest of the blob of this image part.

from_image classmethod

from_image(image: Image, partname: PackURI)

Return an ImagePart instance newly created from image and assigned partname.

Source code in src/docx/parts/image.py
@classmethod
def from_image(cls, image: Image, partname: PackURI):
    """Return an |ImagePart| instance newly created from `image` and assigned
    `partname`."""
    return ImagePart(partname, image.content_type, image.blob, image)

load classmethod

load(
    partname: PackURI,
    content_type: str,
    blob: bytes,
    package: OpcPackage,
)

Called by docx.opc.package.PartFactory to load an image part from a package being opened by Document(...) call.

Source code in src/docx/parts/image.py
@classmethod
def load(cls, partname: PackURI, content_type: str, blob: bytes, package: OpcPackage):
    """Called by ``docx.opc.package.PartFactory`` to load an image part from a
    package being opened by ``Document(...)`` call."""
    return cls(partname, content_type, blob)