From ce39090213de05ef325a1767a40ca53819b45561 Mon Sep 17 00:00:00 2001 From: simpleqt Date: Mon, 30 Nov 2026 14:44:46 +0800 Subject: [PATCH] fix: distinguish EMF images from WMF Pillow reports both WMF and EMF as "WMF", so Image.ext and Image.content_type returned the WMF extension and MIME type for EMF images. Detect the " EMF" signature at offset 40 and map the format to "emf" (image/x-emf). --- src/pptx/parts/image.py | 5 +++++ tests/parts/test_image.py | 9 +++++++++ tests/test_files/generic-icon.emf | Bin 0 -> 5396 bytes 3 files changed, 14 insertions(+) create mode 100644 tests/test_files/generic-icon.emf 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 0000000000000000000000000000000000000000..d0914e003548ff8099862e9b42b5e101cc7f8149 GIT binary patch literal 5396 zcmeHLO>0v@6g{sgjo%3pp}O%A6$C+~3gSXyOIs*dLbd$`ejya?s^F?`S#aYIsFI%` zbm1Qe`U|9jTcx__!i88=c%Jj#Tr<2#^PvwF-c8P%d+*G-cjjg$2@?QVCQcCnF-3%g zN&5isWsJU=`B{yq9RrTCbHoO?HYx+pM}aYROe>HlE8AtXK8<3WzC+G`-05)~W1jkH zaL5LMPoONxTZNQhrkD55#!(z##9)AZ)yZNEa z*cIX=(ID1{cozXtidCalwReqXao5dWO{XrtZtGHwE)gvPj2*HOtVYrUdP;27-z}_l z`tI)fM?6s!Ay53xWoli$M4xpYP~IYc)T{it)ZWf-QU9X;y*6MTXTR8g>3@vk{1xY~ zIDdOrp!ohN{(lX-2ln1s{q+u;+kWG+{C>Xk{h4L?gV>7Wj~_I5mhZ>vNB8-|sQ*hSFumTGlbQCk@|Hxov{3gsO(Fxw*hV@lDQb1`{}sX=>?8ZM?0+AOEZP MM;P1YsXWqu0C+~TrT_o{ literal 0 HcmV?d00001