Tkinter error: Couldn't recognize data in image file
Solution 1
Your code seems right, this is running for me on Windows 7 (Python 3.6):
from tkinter import *
root = Tk()
canv = Canvas(root, width=80, height=80, bg='white')
canv.grid(row=2, column=3)
img = PhotoImage(file="bll.jpg")
canv.create_image(20,20, anchor=NW, image=img)
mainloop()
resulting in this tkinter GUI:
(imgur converted it to bll.png
but this is working for me as well.)
More options:
-
This answer mentions, tkinter is working only with
gif
images. Try using a.gif
image. - If this is not working, use
PIL
as stated in this answer.
Update: Solution with PIL
:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
canv = Canvas(root, width=80, height=80, bg='white')
canv.grid(row=2, column=3)
img = ImageTk.PhotoImage(Image.open("bll.jpg")) # PIL solution
canv.create_image(20, 20, anchor=NW, image=img)
mainloop()
Solution 2
I was getting the same issue. I have windows and Python 3.6. So I found two solutions for this either you use/convert to .png
image (with the same function you have used):
photo = PhotoImage('xyz.png')
l = Label(image = photo)
l.pack()
or if you want to read .jpg
file only then use PIL library to read and display an image like this:
from PIL import ImageTk, Image
img = ImageTk.PhotoImage(Image.open("xyz.jpg"))
l=Label(image=img)
l.pack()
Solution 3
Install PIL/Pillow with:
pip install Pillow
or:
sudo pip install pillow
from PIL import Image
from PIL import ImageTk
import tkinter
image = Image.open('bll.jpg')
image = image.resize((20, 20))
image = ImageTk.PhotoImage(image)
canv = Canvas(root, width=80, height=80, bg='white')
canv.grid(row=2, column=3)
img = PhotoImage(file=image)
Also using .PNG instead of .JPG is better for Tkinter.
Solution 4
Another alternative solution:
filename = ImageTk.PhotoImage(Image.open('imagename.jpeg' ))
background_label = tk.Label(self.root, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
Igor234
Updated on July 29, 2022Comments
-
Igor234 almost 2 years
I'm trying to put a jpg image to a tkinter canvas. tkinter gives me this error:
couldn't recognize data in image file
I use the code from the documentation:
canv = Canvas(root, width=80, height=80, bg='white') canv.grid(row=2, column=3) img = PhotoImage(file="bll.jpg") canv.create_image(20,20, anchor=NW, image=img)
Same thing with png images. Even tried to put an image into a label widget, but got the same error. What's wrong?
I am using Python 3 on Mac. Python file and image are in the same folder.
-
Batata over 4 yearsThis helped. Although I would suggest adding window and mainloop() to actually display the image.
window = Tk()
and ending with:window.mainloop()
-
Russell Smith over 2 yearsIf the problem was related to the file path, the OP would get a different error. The error in the question is not because the file couldn't be found.
-
Admin over 2 yearsAs it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
-
Matiiss over 2 yearsthose are not Python comments