Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poor quality exports of certain WMFs/EMFs #8524

Open
burghoff opened this issue Nov 2, 2024 · 3 comments · May be fixed by #8536
Open

Poor quality exports of certain WMFs/EMFs #8524

burghoff opened this issue Nov 2, 2024 · 3 comments · May be fixed by #8536
Labels

Comments

@burghoff
Copy link

burghoff commented Nov 2, 2024

What did you do?

Export WMFs and EMFs as PNGs using the following script:

filein = 'image131.emf'; fileout = 'image131.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)
        if 'info' in im.__dict__ and "dpi" in im.info and isinstance(im.info["dpi"],tuple) and len(im.info["dpi"]) == 2:
            new_height = int((new_width / width * im.info["dpi"][0]/im.info["dpi"][1]) * height)
        resized_image = im.resize((new_width, new_height), Image.LANCZOS)
        resized_image.save(fileout)
        print('PIL export of '+filein)

For some files, namely smaller EMFs and WMFs, the export quality is poor. These are files that look good in vector viewers like Powerpoint, Inkscape, or Illustrator.

image43
image131

tests.zip

What actually happened?

It appears that the image is being loaded at a very low resolution to start with, and then looks bad due to upscaling. If there was a way to specify the DPI during the load, that would probably solve the issue.

What are your OS, Python and Pillow versions?

  • OS: Windows 10
  • 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
--------------------------------------------------------------------
@radarhere
Copy link
Member

If there was a way to specify the DPI during the load, that would probably solve the issue.

You can already do this for WMF images. At https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#wmf-emf, it mentions that you can

from PIL import Image

with Image.open("drawing.wmf") as im:
    im.load(dpi=144)

It will only work for WMF images at the moment, but if that's all you're after, we can extend it to work for EMF images as well.

@radarhere radarhere linked a pull request Nov 5, 2024 that will close this issue
@radarhere
Copy link
Member

I've created #8536 to resolve this by adding that feature for EMF images.

@burghoff
Copy link
Author

burghoff commented Nov 5, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants