Paper Office
paper-docxAPI referenceimage.image

Image

paper-docx 0.2.0 API reference

Graphical image stream such as JPEG, PNG, or GIF with properties and methods required by ImagePart.

Image(blob: bytes, filename: str, image_header: BaseImageHeader)

Attributes

blob

attributeblob

The bytes of the image 'file'.

content_type

attributecontent_typestr

MIME content type for this image, e.g. 'image/jpeg' for a JPEG image.

ext

attributeext

The file extension for the image.

If an actual one is available from a load filename it is used. Otherwise a canonical extension is assigned based on the content type. Does not contain the leading period, e.g. 'jpg', not '.jpg'.

filename

attributefilename

Original image file name, if loaded from disk, or a generic filename if loaded from an anonymous stream.

height

attributeheightInches

A Length value representing the native height of the image, calculated from the values of px_height and vert_dpi.

horz_dpi

attributehorz_dpiint

Integer dots per inch for the width of this image.

Defaults to 72 when not present in the file, as is often the case.

px_height

attributepx_heightint

The vertical pixel dimension of the image.

px_width

attributepx_widthint

The horizontal pixel dimension of the image.

sha1

attributesha1

SHA1 hash digest of the image blob.

vert_dpi

attributevert_dpiint

Integer dots per inch for the height of this image.

Defaults to 72 when not present in the file, as is often the case.

width

attributewidthInches

A Length value representing the native width of the image, calculated from the values of px_width and horz_dpi.

Functions

from_blob

funcfrom_blob(blob: bytes) -> Image

Return a new Image subclass instance parsed from the image binary contained in blob.

paramblobbytes

Returns

docx.image.image.Image

from_file

funcfrom_file(image_descriptor: str | IO[bytes])

Return a new Image subclass instance loaded from the image file identified by image_descriptor, a path or file-like object.

paramimage_descriptorstr | IO[bytes]

scaled_dimensions

funcscaled_dimensions(width: int | Length | None = None, height: int | Length | None = None) -> Tuple[Length, Length]

(cx, cy) pair representing scaled dimensions of this image.

The native dimensions of the image are scaled by applying the following rules to the width and height arguments.

  • If both width and height are specified, the return value is (width, height); no scaling is performed.
  • If only one is specified, it is used to compute a scaling factor that is then applied to the unspecified dimension, preserving the aspect ratio of the image.
  • If both width and height are None, the native dimensions are returned.

The native dimensions are calculated using the dots-per-inch (dpi) value embedded in the image, defaulting to 72 dpi if no value is specified, as is often the case. The returned values are both Length objects.

paramwidthint | Length | None
= None
paramheightint | Length | None
= None

Returns

typing.Tuple[docx.shared.Length, docx.shared.Length]

On this page