You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed an error when I wanted to include a PNG image in palette mode with alpha channel into a tkinter application.
Since I wanted to resize it before displaying it, I used Pillow.
However if I execute the code below (reproduce.zip) I see this:
It shows the following 3 images:
The first image is created using Image.open and ImageTk.PhotoImage, the transparent part is black.
The second image shows the same image which has been converted to RGBA mode and then saved (also with Pillow), which works.
The third image shows the tkinter.PhotoImage version which also works fine.
Uncommenting the explicit conversion to RGBA also fixes the problem, which is my current workaround.
However I think this should not be necessary.
The text was updated successfully, but these errors were encountered:
BenediktO
changed the title
Alpha channel ignored in tkinter for PNGs in palette mode
Alpha channel ignored in tkinter using PIL.Image.open and PIL.ImageTk.PhotoImage for PNGs in palette mode
Sep 4, 2022
Looking at ImageTk.PhotoImage, if it is passed an image with a palette, it automatically converts the image to the mode of the palette before passing it to tkinter:
However, your first image is loaded with a palette with mode RGB, not RGBA as you might expect.
This was discussed in #6348, and #6352 added a new function image.apply_transparency() to convert the palette of PNG images from RGB to RGBA in Pillow 9.2.0.
fromtkinterimportTk, Label, PhotoImagefromPILimportImage, ImageTkroot=Tk()
pil_image=Image.open('loading.png')
print(pil_image.mode, pil_image.palette.mode) # P RGBpil_image.apply_transparency()
print(pil_image.mode, pil_image.palette.mode) # P RGBAimage1=ImageTk.PhotoImage(pil_image)
Label(root, image=image1).grid(row=0, column=0, padx=5, pady=5)
What did you do?
I opened a PNG image (palette mode with alpha channel) using pillow and passed it to tkinter.
What did you expect to happen?
It should work just like using tkinter.PhotoImage.
What actually happened?
The alpha channel is ignored.
What are your OS, Python and Pillow versions?
5.10.0-17-amd64 #1 SMP Debian 5.10.136-1 (2022-08-13) x86_64 GNU/Linux
Python 3.9.2
9.0.1
I noticed an error when I wanted to include a PNG image in palette mode with alpha channel into a tkinter application.
Since I wanted to resize it before displaying it, I used Pillow.
However if I execute the code below (reproduce.zip) I see this:
It shows the following 3 images:
Image.open
andImageTk.PhotoImage
, the transparent part is black.tkinter.PhotoImage
version which also works fine.Uncommenting the explicit conversion to RGBA also fixes the problem, which is my current workaround.
However I think this should not be necessary.
The text was updated successfully, but these errors were encountered: