Reputation: 41
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()
Upvotes: 2
Views: 14870
Reputation: 23
for me it only worked after adding the line: label.image= display
Whole solution for your problem:
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.image = display
label.pack()
root.mainloop()
Upvotes: 1
Reputation: 41
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.
Upvotes: 2
Reputation: 1619
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))
Upvotes: 5