Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/pptx/parts/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ def ext(self) -> str:
"""
ext_map = {
"BMP": "bmp",
"EMF": "emf",
"GIF": "gif",
"JPEG": "jpg",
"PNG": "png",
Expand Down Expand Up @@ -266,6 +267,10 @@ def _pil_props(self) -> tuple[str | None, tuple[int, int], tuple[int, int] | Non
stream = io.BytesIO(self._blob)
pil_image = PIL_Image.open(stream) # pyright: ignore[reportUnknownMemberType]
format = pil_image.format
# Pillow reports both WMF and EMF as "WMF"; EMF is distinguished by
# the " EMF" signature at offset 40 and uses a different MIME type.
if format == "WMF" and self._blob[40:44] == b" EMF":
format = "EMF"
width_px, height_px = pil_image.size
dpi = cast(
"tuple[int, int] | None",
Expand Down
9 changes: 9 additions & 0 deletions tests/parts/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
images_pptx_path = absjoin(test_file_dir, "with_images.pptx")

test_image_path = absjoin(test_file_dir, "python-icon.jpeg")
test_emf_path = absjoin(test_file_dir, "generic-icon.emf")
test_eps_path = absjoin(test_file_dir, "cdw-logo.eps")
new_image_path = absjoin(test_file_dir, "monty-truth.png")

Expand Down Expand Up @@ -125,6 +126,12 @@ def it_knows_its_canonical_filename_extension(self, ext_fixture):
image, expected_value = ext_fixture
assert image.ext == expected_value

def it_distinguishes_emf_from_wmf(self):
image = Image.from_file(test_emf_path)
assert image._format == "EMF"
assert image.ext == "emf"
assert image.content_type == "image/x-emf"

def it_knows_its_dpi(self, dpi_fixture):
image, expected_value = dpi_fixture
assert image.dpi == expected_value
Expand Down Expand Up @@ -155,6 +162,7 @@ def blob_fixture(self):
@pytest.fixture(
params=[
("BMP", "image/bmp"),
("EMF", "image/x-emf"),
("GIF", "image/gif"),
("JPEG", "image/jpeg"),
("PNG", "image/png"),
Expand Down Expand Up @@ -186,6 +194,7 @@ def dpi_fixture(self, request, _pil_props_):
@pytest.fixture(
params=[
("BMP", "bmp"),
("EMF", "emf"),
("GIF", "gif"),
("JPEG", "jpg"),
("PNG", "png"),
Expand Down
Binary file added tests/test_files/generic-icon.emf
Binary file not shown.