Fix bad image quality Pillow Tkinter macOS - Python

Viewed 30

I'm building a GUI app in python with Tkinter and I'm using the Pillow module to add images. I just moved on to a new MacBook Pro with a better screen resolution than my previous laptop and noticed the resolution of the images on the GUI is very bad. Even if the original PNG file quality is okay.

Here is my code:

from tkinter import Tk, Frame, Label
from PIL import Image, ImageTk

root = Tk()

global_frame = Frame(root, border=10)
global_frame.grid()

img = Image.open("/Users/marinnagy/Documents/image.png")
img_tk = ImageTk.PhotoImage(img)

Label(global_frame, image=img_tk).pack()
Label(global_frame, text="I am a label").pack()

root.mainloop()

And here is the result with a 70x15px image:

first example

I tried to use a bigger image (700x150px) and downscale it by adding img = img.resize((70, 15)) just after opening the image file. I also tried to add the Image.LANCZOS option but the result is the same, if not worst:

second example

Do you know what's wrong and how I can display a good quality image in tkinter on macOS? Maybe a specific file format or another way to open the file?

EDIT: I saw a similar issue where the whole window resolution (image, label, button...) is low on macOS. This problem can be fixed by adding NSHighResolutionCapable parameter to the info.plist file. I checked but didn't find any similar thing for the image rendering, maybe that can help.

1 Answers

I just testet one of my apps on my mac 10.14 but on a bit older MacBook

Any yeah, I'd say the quality is bit lower, but it's hard to tell as I had to create screenshots on windows and pc to compare.

anyways, maybe this helps:

from PIL import Image

file = 'myfile.png'
img = Image.open(file)
small = (70, 15)
img.thumbnail(small)

Can't test it at the moment, but I was told that the thumbnail function on a PIL Image creates higher quality outputs.

Related