Tkinter error: Couldn't recognize data in image file Tkinter error: Couldn't recognize data in image file python python

Tkinter error: Couldn't recognize data in image file


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:

GUI with this image as bll.jpg: image

(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, Imageroot = Tk()canv = Canvas(root, width=80, height=80, bg='white')canv.grid(row=2, column=3)img = ImageTk.PhotoImage(Image.open("bll.jpg"))  # PIL solutioncanv.create_image(20, 20, anchor=NW, image=img)mainloop()


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, Imageimg = ImageTk.PhotoImage(Image.open("xyz.jpg"))  l=Label(image=img)l.pack()


Install PIL/Pillow with:

pip install Pillow

or:

sudo pip install pillow
from PIL import Imagefrom PIL import ImageTkimport tkinterimage = 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.