Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display .png image with Tkinter and PIL

I try to display .png file in Tkinter Label, but in effect I get just empty space in place where image should be displayed. It's very simple code and i have no idea what is wrong.

from Tkinter import *
from PIL import Image, ImageTk

root = Tk()

image = Image.open('image.png')
display = ImageTk.PhotoImage(Image.open(image))

label = Label(root, image=display)
label.pack()

root.mainloop()
like image 998
Emiel Avatar asked Dec 07 '25 07:12

Emiel


2 Answers

You're calling Image.open() twice. It's enough to call it once. Use:

display = ImageTk.PhotoImage(image)

instead of:

display = ImageTk.PhotoImage(Image.open(image))
like image 51
Michiel Overtoom Avatar answered Dec 08 '25 20:12

Michiel Overtoom


I managed to resolve this problem in this way:

image = Image.open('image.png').convert("RGB")

I'm not sure if it's correct, but it works.

like image 29
Emiel Avatar answered Dec 08 '25 21:12

Emiel