diff --git a/src/pptx/parts/image.py b/src/pptx/parts/image.py index 9be5d02d6..8ebd71d5d 100644 --- a/src/pptx/parts/image.py +++ b/src/pptx/parts/image.py @@ -225,6 +225,7 @@ def ext(self) -> str: """ ext_map = { "BMP": "bmp", + "EMF": "emf", "GIF": "gif", "JPEG": "jpg", "PNG": "png", @@ -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", diff --git a/tests/parts/test_image.py b/tests/parts/test_image.py index 386e3fce9..9289fbe90 100644 --- a/tests/parts/test_image.py +++ b/tests/parts/test_image.py @@ -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") @@ -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 @@ -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"), @@ -186,6 +194,7 @@ def dpi_fixture(self, request, _pil_props_): @pytest.fixture( params=[ ("BMP", "bmp"), + ("EMF", "emf"), ("GIF", "gif"), ("JPEG", "jpg"), ("PNG", "png"), diff --git a/tests/test_files/generic-icon.emf b/tests/test_files/generic-icon.emf new file mode 100644 index 000000000..d0914e003 Binary files /dev/null and b/tests/test_files/generic-icon.emf differ