Image header parser for WMF (Windows Metafile) images.
A bare WMF records only drawing commands in metafile units and says nothing about how
large the result should be. The physical size comes from the Aldus Placeable Metafile
header, a 22-byte prefix carrying a bounding box and the number of metafile units per
inch. Only a placeable WMF is recognized here, because a bare one gives us nothing to
size the picture with.
Wmf
Wmf(
px_width: int,
px_height: int,
horz_dpi: int,
vert_dpi: int,
orientation: int = 1,
)
Bases: BaseImageHeader
Image header parser for WMF images having an Aldus Placeable Metafile header.
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
MIME content type for this image, unconditionally image/x-wmf for WMF
images.
default_ext
property
Default filename extension, always 'wmf' for WMF images.
from_stream
classmethod
Return a Wmf instance with header properties parsed from stream.
Source code in src/docx/image/wmf.py
| @classmethod
def from_stream(cls, stream):
"""Return a |Wmf| instance with header properties parsed from `stream`."""
stream.seek(0)
header = stream.read(_APM_HEADER_LENGTH)
if len(header) < _APM_HEADER_LENGTH:
raise InvalidImageStreamError("unexpected end of WMF image stream")
key, _hwmf, left, top, right, bottom, inch, _reserved, _checksum = _APM_HEADER.unpack(
header
)
if key != _APM_KEY:
raise InvalidImageStreamError(
"WMF image has no Aldus Placeable Metafile header, so its display size is unknown"
)
if inch == 0:
raise InvalidImageStreamError("WMF image declares zero metafile units per inch")
# -- the bounding box is in metafile units, `inch` of them to the inch --
inch_width = abs(right - left) / inch
inch_height = abs(bottom - top) / inch
if inch_width == 0 or inch_height == 0:
raise InvalidImageStreamError("WMF image has a zero-size bounding box")
px_width = int(round(inch_width * _WMF_DPI))
px_height = int(round(inch_height * _WMF_DPI))
return cls(px_width, px_height, _WMF_DPI, _WMF_DPI)
|