Skip to content

png

png

Png

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

Bases: BaseImageHeader

Image header parser for PNG images.

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/png for PNG images.

default_ext property

default_ext

Default filename extension, always 'png' for PNG images.

from_stream classmethod

from_stream(stream)

Return a Png instance having header properties parsed from image in stream.

Source code in src/docx/image/png.py
@classmethod
def from_stream(cls, stream):
    """Return a |Png| instance having header properties parsed from image in
    `stream`."""
    parser = _PngParser.parse(stream)

    px_width = parser.px_width
    px_height = parser.px_height
    horz_dpi = parser.horz_dpi
    vert_dpi = parser.vert_dpi

    return cls(px_width, px_height, horz_dpi, vert_dpi)

_PngParser

_PngParser(chunks)

Parses a PNG image stream to extract the image properties found in its chunks.

Source code in src/docx/image/png.py
def __init__(self, chunks):
    super(_PngParser, self).__init__()
    self._chunks = chunks

px_width property

px_width

The number of pixels in each row of the image.

px_height property

px_height

The number of stacked rows of pixels in the image.

horz_dpi property

horz_dpi

Integer dots per inch for the width of this image.

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

vert_dpi property

vert_dpi

Integer dots per inch for the height of this image.

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

parse classmethod

parse(stream)

Return a _PngParser instance containing the header properties parsed from the PNG image in stream.

Source code in src/docx/image/png.py
@classmethod
def parse(cls, stream):
    """Return a |_PngParser| instance containing the header properties parsed from
    the PNG image in `stream`."""
    chunks = _Chunks.from_stream(stream)
    return cls(chunks)

_Chunks

_Chunks(chunk_iterable)

Collection of the chunks parsed from a PNG image stream.

Source code in src/docx/image/png.py
def __init__(self, chunk_iterable):
    super(_Chunks, self).__init__()
    self._chunks = list(chunk_iterable)

IHDR property

IHDR

IHDR chunk in PNG image.

pHYs property

pHYs

PHYs chunk in PNG image, or None if not present.

from_stream classmethod

from_stream(stream)

Return a _Chunks instance containing the PNG chunks in stream.

Source code in src/docx/image/png.py
@classmethod
def from_stream(cls, stream):
    """Return a |_Chunks| instance containing the PNG chunks in `stream`."""
    chunk_parser = _ChunkParser.from_stream(stream)
    chunks = list(chunk_parser.iter_chunks())
    return cls(chunks)

_ChunkParser

_ChunkParser(stream_rdr)

Extracts chunks from a PNG image stream.

Source code in src/docx/image/png.py
def __init__(self, stream_rdr):
    super(_ChunkParser, self).__init__()
    self._stream_rdr = stream_rdr

from_stream classmethod

from_stream(stream)

Return a _ChunkParser instance that can extract the chunks from the PNG image in stream.

Source code in src/docx/image/png.py
@classmethod
def from_stream(cls, stream):
    """Return a |_ChunkParser| instance that can extract the chunks from the PNG
    image in `stream`."""
    stream_rdr = StreamReader(stream, BIG_ENDIAN)
    return cls(stream_rdr)

iter_chunks

iter_chunks()

Generate a _Chunk subclass instance for each chunk in this parser's PNG stream, in the order encountered in the stream.

Source code in src/docx/image/png.py
def iter_chunks(self):
    """Generate a |_Chunk| subclass instance for each chunk in this parser's PNG
    stream, in the order encountered in the stream."""
    for chunk_type, offset in self._iter_chunk_offsets():
        chunk = _ChunkFactory(chunk_type, self._stream_rdr, offset)
        yield chunk

_Chunk

_Chunk(chunk_type)

Base class for specific chunk types.

Also serves as the default chunk type.

Source code in src/docx/image/png.py
def __init__(self, chunk_type):
    super(_Chunk, self).__init__()
    self._chunk_type = chunk_type

type_name property

type_name

The chunk type name, e.g. 'IHDR', 'pHYs', etc.

from_offset classmethod

from_offset(chunk_type, stream_rdr, offset)

Return a default _Chunk instance that only knows its chunk type.

Source code in src/docx/image/png.py
@classmethod
def from_offset(cls, chunk_type, stream_rdr, offset):
    """Return a default _Chunk instance that only knows its chunk type."""
    return cls(chunk_type)

_IHDRChunk

_IHDRChunk(chunk_type, px_width, px_height)

Bases: _Chunk

IHDR chunk, contains the image dimensions.

Source code in src/docx/image/png.py
def __init__(self, chunk_type, px_width, px_height):
    super(_IHDRChunk, self).__init__(chunk_type)
    self._px_width = px_width
    self._px_height = px_height

from_offset classmethod

from_offset(chunk_type, stream_rdr, offset)

Return an _IHDRChunk instance containing the image dimensions extracted from the IHDR chunk in stream at offset.

Source code in src/docx/image/png.py
@classmethod
def from_offset(cls, chunk_type, stream_rdr, offset):
    """Return an _IHDRChunk instance containing the image dimensions extracted from
    the IHDR chunk in `stream` at `offset`."""
    px_width = stream_rdr.read_long(offset)
    px_height = stream_rdr.read_long(offset, 4)
    return cls(chunk_type, px_width, px_height)

_ChunkFactory

_ChunkFactory(chunk_type, stream_rdr, offset)

Return a _Chunk subclass instance appropriate to chunk_type parsed from stream_rdr at offset.

Source code in src/docx/image/png.py
def _ChunkFactory(chunk_type, stream_rdr, offset):
    """Return a |_Chunk| subclass instance appropriate to `chunk_type` parsed from
    `stream_rdr` at `offset`."""
    chunk_cls_map = {
        PNG_CHUNK_TYPE.IHDR: _IHDRChunk,
        PNG_CHUNK_TYPE.pHYs: _pHYsChunk,
    }
    chunk_cls = chunk_cls_map.get(chunk_type, _Chunk)
    return chunk_cls.from_offset(chunk_type, stream_rdr, offset)