Skip to content

webp

webp

Image header parser for WebP images.

A WebP file is a RIFF container: "RIFF", a four-byte file size, "WEBP", and then a chunk whose FourCC says which of the three bitstream variants follows. The image dimensions live in a different place in each, which is the whole of the work here.

Word renders WebP natively from Microsoft 365 / Word 2021 onward. Earlier versions show a placeholder instead, so a document that has to open in Word 2019 or earlier should carry PNG or JPEG.

Webp

Webp(
    px_width: int,
    px_height: int,
    horz_dpi: int,
    vert_dpi: int,
    orientation: int = 1,
)

Bases: BaseImageHeader

Image header parser for WebP images.

Note that the WebP format carries no resolution (DPI) information. Both horizontal and vertical DPI default to 72.

Source code in src/docx/image/image.py
def __init__(
    self,
    px_width: int,
    px_height: int,
    horz_dpi: int,
    vert_dpi: int,
    orientation: int = 1,
):
    self._px_width = px_width
    self._px_height = px_height
    self._horz_dpi = horz_dpi
    self._vert_dpi = vert_dpi
    self._orientation = orientation

content_type property

content_type

MIME content type for this image, unconditionally image/webp for WebP images.

default_ext property

default_ext

Default filename extension, always 'webp' for WebP images.

from_stream classmethod

from_stream(stream)

Return Webp instance having header properties parsed from the WebP image in stream.

Source code in src/docx/image/webp.py
@classmethod
def from_stream(cls, stream):
    """Return |Webp| instance having header properties parsed from the WebP image in
    `stream`."""
    px_width, px_height = cls._dimensions_from_stream(stream)
    return cls(px_width, px_height, _WEBP_DPI, _WEBP_DPI)