Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

EMF aspect ratio issues

Open burghoff opened this issue 1 year ago • 6 comments

What did you do?

Attempted to use imsize and save to convert an EMF to a PNG. For certain EMFs, the final aspect ratio is incorrect.

image94.zip

filein = 'image100.emf'
conversion_path = 'image100.png'
from PIL import Image
with open(filein, "rb") as file:
    with Image.open(file) as im:
        width, height = im.size
        new_width = 400
        new_height = int((new_width / width) * height)
        resized_image = im.resize((new_width, new_height), Image.LANCZOS)
        resized_image.save(conversion_path)

What did you expect to happen?

It should have been converted to PNG with the original aspect ratio.

What actually happened?

It converted with the wrong aspect ratio, as shown here: image94

It seems to be inferring the wrong width and height. I attempted getting the width and height using various low-level means, but that also didn't work.

What are your OS, Python and Pillow versions?

  • OS: Windows 10 Home, version 22H2, build 19045.5011
  • Python: 3.9.20
  • Pillow: 10.4.0
--------------------------------------------------------------------
Pillow 10.4.0
Python 3.9.20 (main, Oct  3 2024, 07:38:01) [MSC v.1929 64 bit (AMD64)]
--------------------------------------------------------------------
Python executable is C:\Users\burgh\anaconda3\python.exe
System Python files loaded from C:\Users\burgh\anaconda3
--------------------------------------------------------------------
Python Pillow modules loaded from C:\Users\burgh\anaconda3\lib\site-packages\PIL
Binary Pillow modules loaded from C:\Users\burgh\anaconda3\lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.4.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.12
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for 9.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.1
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------
--------------------------------------------------------------------
Pillow 10.4.0
Python 3.9.20 (main, Oct  3 2024, 07:38:01) [MSC v.1929 64 bit (AMD64)]
--------------------------------------------------------------------
Python executable is C:\Users\burgh\anaconda3\python.exe
System Python files loaded from C:\Users\burgh\anaconda3
--------------------------------------------------------------------
Python Pillow modules loaded from C:\Users\burgh\anaconda3\lib\site-packages\PIL
Binary Pillow modules loaded from C:\Users\burgh\anaconda3\lib\site-packages\PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 10.4.0
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.12
--- WEBP support ok, loaded 1.3.2
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for 9.0
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.2
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.13
--- LIBTIFF support ok, loaded 4.5.1
*** RAQM (Bidirectional Text) support not installed
*** LIBIMAGEQUANT (Quantization method) support not installed
*** XCB (X protocol) support not installed
--------------------------------------------------------------------

burghoff avatar Oct 12 '24 17:10 burghoff

Investigating, I found that the DPI has two dimensions in your image.

See what you think of this.

filein = 'image100.emf'
conversion_path = 'image100.png'
from PIL import Image
with open(filein, "rb") as file:
    with Image.open(file) as im:
        width, height = im.size
        new_width = 400
        new_height = (new_width / width) * height
        if len(im.info["dpi"]) == 2:
            new_height /= im.info["dpi"][1] / im.info["dpi"][0]
        new_height = int(new_height)
        resized_image = im.resize((new_width, new_height), Image.LANCZOS)
        resized_image.save(conversion_path)

radarhere avatar Oct 13 '24 13:10 radarhere

Much better (thanks), but still not completely correct. This is how it appears in Inkscape & Powerpoint: image. Using the non-uniform DPI gives a correct height (.388 in), but an incorrect width (2.87 in, when it should be 2.14 in).

I should point out that even if you skip the resizing, you get the same issue. I'm not sure if this is intended behavior or not—to me a non-uniform DPI should be taken into account when saving, but maybe that's the intended behavior.

burghoff avatar Oct 13 '24 14:10 burghoff

Would you happen to have any images with different DPIs that we could add to our test suite, and distribute under the Pillow license?

radarhere avatar Oct 14 '24 06:10 radarhere

Your second comment is not as helpful as it could be, as you're talking about a different image. Would you be able to express your expectations of the pixel dimensions after opening the first image?

radarhere avatar Oct 19 '24 07:10 radarhere

Sorry, I mistakenly thought that was the one I had uploaded since the code refers to it. Here is the one I was talking about: image100.zip

image94 (what I posted before) ends up being corrected to the correct size, but image100 is still a little off.

burghoff avatar Oct 19 '24 11:10 burghoff

Thanks. Using the code I posted earlier on image100.emf, I get

image100

I've created #8485 to resolve this. With that and my earlier code, I get a result I believe to be correct.

image100withfix

radarhere avatar Oct 19 '24 20:10 radarhere