Skip to content

tiff

tiff

Tiff

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

Bases: BaseImageHeader

Image header parser for TIFF images.

Handles both big and little endian byte ordering.

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

Return the MIME type of this TIFF image, unconditionally the string image/tiff.

default_ext property

default_ext

Default filename extension, always 'tiff' for TIFF images.

from_stream classmethod

from_stream(stream)

Return a Tiff instance containing the properties of the TIFF image in stream.

Source code in src/docx/image/tiff.py
@classmethod
def from_stream(cls, stream):
    """Return a |Tiff| instance containing the properties of the TIFF image in
    `stream`."""
    parser = _TiffParser.parse(stream)

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

    return cls(px_width, px_height, horz_dpi, vert_dpi, orientation)

_TiffParser

_TiffParser(ifd_entries)

Parses a TIFF image stream to extract the image properties found in its main image file directory (IFD)

Source code in src/docx/image/tiff.py
def __init__(self, ifd_entries):
    super(_TiffParser, self).__init__()
    self._ifd_entries = ifd_entries

horz_dpi property

horz_dpi

The horizontal dots per inch value calculated from the XResolution and ResolutionUnit tags of the IFD; defaults to 72 if those tags are not present.

vert_dpi property

vert_dpi

The vertical dots per inch value calculated from the XResolution and ResolutionUnit tags of the IFD; defaults to 72 if those tags are not present.

orientation property

orientation: int

The Orientation tag value, or 1 when the tag is absent or unparseable.

1 means the stored pixels are already in display order, which is what every value outside the documented 1..8 range is treated as: an orientation nobody can act on is better ignored than guessed at.

px_height property

px_height

The number of stacked rows of pixels in the image, None if the IFD contains no ImageLength tag, the expected case when the TIFF is embeded in an Exif image.

px_width property

px_width

The number of pixels in each row in the image, None if the IFD contains no ImageWidth tag, the expected case when the TIFF is embeded in an Exif image.

parse classmethod

parse(stream)

Return an instance of _TiffParser containing the properties parsed from the TIFF image in stream.

Source code in src/docx/image/tiff.py
@classmethod
def parse(cls, stream):
    """Return an instance of |_TiffParser| containing the properties parsed from the
    TIFF image in `stream`."""
    stream_rdr = cls._make_stream_reader(stream)
    ifd0_offset = stream_rdr.read_long(4)
    ifd_entries = _IfdEntries.from_stream(stream_rdr, ifd0_offset)
    return cls(ifd_entries)

_IfdEntries

_IfdEntries(entries)

Image File Directory for a TIFF image, having mapping (dict) semantics allowing "tag" values to be retrieved by tag code.

Source code in src/docx/image/tiff.py
def __init__(self, entries):
    super(_IfdEntries, self).__init__()
    self._entries = entries

from_stream classmethod

from_stream(stream, offset)

Return a new _IfdEntries instance parsed from stream starting at offset.

Source code in src/docx/image/tiff.py
@classmethod
def from_stream(cls, stream, offset):
    """Return a new |_IfdEntries| instance parsed from `stream` starting at
    `offset`."""
    ifd_parser = _IfdParser(stream, offset)
    entries = {e.tag: e.value for e in ifd_parser.iter_entries()}
    return cls(entries)

get

get(tag_code, default=None)

Return value of IFD entry having tag matching tag_code, or default if no matching tag found.

Source code in src/docx/image/tiff.py
def get(self, tag_code, default=None):
    """Return value of IFD entry having tag matching `tag_code`, or `default` if no
    matching tag found."""
    return self._entries.get(tag_code, default)

_IfdParser

_IfdParser(stream_rdr, offset)

Service object that knows how to extract directory entries from an Image File Directory (IFD)

Source code in src/docx/image/tiff.py
def __init__(self, stream_rdr, offset):
    super(_IfdParser, self).__init__()
    self._stream_rdr = stream_rdr
    self._offset = offset

iter_entries

iter_entries()

Generate an _IfdEntry instance corresponding to each entry in the directory.

Source code in src/docx/image/tiff.py
def iter_entries(self):
    """Generate an |_IfdEntry| instance corresponding to each entry in the
    directory."""
    for idx in range(self._entry_count):
        dir_entry_offset = self._offset + 2 + (idx * 12)
        ifd_entry = _IfdEntryFactory(self._stream_rdr, dir_entry_offset)
        yield ifd_entry

_IfdEntry

_IfdEntry(tag_code, value)

Base class for IFD entry classes.

Subclasses are differentiated by value type, e.g. ASCII, long int, etc.

Source code in src/docx/image/tiff.py
def __init__(self, tag_code, value):
    super(_IfdEntry, self).__init__()
    self._tag_code = tag_code
    self._value = value

tag property

tag

Short int code that identifies this IFD entry.

value property

value

Value of this tag, its type being dependent on the tag.

from_stream classmethod

from_stream(stream_rdr, offset)

Return an _IfdEntry subclass instance containing the tag and value of the tag parsed from stream_rdr at offset.

Note this method is common to all subclasses. Override the _parse_value() method to provide distinctive behavior based on field type.

Source code in src/docx/image/tiff.py
@classmethod
def from_stream(cls, stream_rdr, offset):
    """Return an |_IfdEntry| subclass instance containing the tag and value of the
    tag parsed from `stream_rdr` at `offset`.

    Note this method is common to all subclasses. Override the ``_parse_value()``
    method to provide distinctive behavior based on field type.
    """
    tag_code = stream_rdr.read_short(offset, 0)
    value_count = stream_rdr.read_long(offset, 4)
    value_offset = stream_rdr.read_long(offset, 8)
    value = cls._parse_value(stream_rdr, offset, value_count, value_offset)
    return cls(tag_code, value)

_AsciiIfdEntry

_AsciiIfdEntry(tag_code, value)

Bases: _IfdEntry

IFD entry having the form of a NULL-terminated ASCII string.

Source code in src/docx/image/tiff.py
def __init__(self, tag_code, value):
    super(_IfdEntry, self).__init__()
    self._tag_code = tag_code
    self._value = value

_ShortIfdEntry

_ShortIfdEntry(tag_code, value)

Bases: _IfdEntry

IFD entry expressed as a short (2-byte) integer.

Source code in src/docx/image/tiff.py
def __init__(self, tag_code, value):
    super(_IfdEntry, self).__init__()
    self._tag_code = tag_code
    self._value = value

_LongIfdEntry

_LongIfdEntry(tag_code, value)

Bases: _IfdEntry

IFD entry expressed as a long (4-byte) integer.

Source code in src/docx/image/tiff.py
def __init__(self, tag_code, value):
    super(_IfdEntry, self).__init__()
    self._tag_code = tag_code
    self._value = value

_RationalIfdEntry

_RationalIfdEntry(tag_code, value)

Bases: _IfdEntry

IFD entry expressed as a numerator, denominator pair.

Source code in src/docx/image/tiff.py
def __init__(self, tag_code, value):
    super(_IfdEntry, self).__init__()
    self._tag_code = tag_code
    self._value = value

_IfdEntryFactory

_IfdEntryFactory(stream_rdr, offset)

Return an _IfdEntry subclass instance containing the value of the directory entry at offset in stream_rdr.

Source code in src/docx/image/tiff.py
def _IfdEntryFactory(stream_rdr, offset):
    """Return an |_IfdEntry| subclass instance containing the value of the directory
    entry at `offset` in `stream_rdr`."""
    ifd_entry_classes = {
        TIFF_FLD.ASCII: _AsciiIfdEntry,
        TIFF_FLD.SHORT: _ShortIfdEntry,
        TIFF_FLD.LONG: _LongIfdEntry,
        TIFF_FLD.RATIONAL: _RationalIfdEntry,
    }
    field_type = stream_rdr.read_short(offset, 2)
    EntryCls = ifd_entry_classes.get(field_type, _IfdEntry)
    return EntryCls.from_stream(stream_rdr, offset)