17

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.

bastelflp
  • 7,944
  • 5
  • 30
  • 65
Igor234
  • 309
  • 1
  • 2
  • 11

10 Answers10

19

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, 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()
bastelflp
  • 7,944
  • 5
  • 30
  • 65
13

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()
David Maze
  • 94,671
  • 18
  • 109
  • 144
Nachiket
  • 162
  • 1
  • 12
  • This helped. Although I would suggest adding window and mainloop() to actually display the image. `window = Tk()` and ending with: `window.mainloop()` – Parikshit Jan 13 '20 at 18:12
7

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.

0xdb
  • 3,364
  • 1
  • 18
  • 35
SF12 Study
  • 355
  • 3
  • 18
1

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)
The Amateur Coder
  • 596
  • 1
  • 8
  • 26
0

This error could possibly happen because of relative file path or non-English characters in filepath. So I made this function which works very good in windows and with any kinds of file paths:

def loadrelimages(relativepath):
    from PIL import ImageTk, Image
    import os
    directory_path = os.path.dirname(__file__)
    file_path = os.path.join(directory_path, relativepath)
    img = ImageTk.PhotoImage(Image.open(file_path.replace('\\', "/")))  
    return img

For example, load photo_2021-08-16_18-44-28.jpg which is at the same directory with this code:

from tkinter import *
import os

def loadrelimages(relativepath):
    from PIL import ImageTk, Image
    import os
    directory_path = os.path.dirname(__file__)
    file_path = os.path.join(directory_path, relativepath)
    img = ImageTk.PhotoImage(Image.open(file_path.replace('\\',"/")))  
    return img

root = Tk()

canvas = Canvas(root, width=500, height=500)
canvas.pack()

loadedimage=loadrelimages('photo_2021-08-16_18-44-28.jpg')
canvas.create_image(250, 250, image=loadedimage)

root.mainloop()
Flair
  • 2,123
  • 1
  • 24
  • 39
team meryb
  • 37
  • 3
  • If 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. – Bryan Oakley Dec 14 '21 at 15:28
0

Using Python package Pillow worked for me.

Just do pip install pillow.

Then use ImageTk.PhotoImage(Image.open("Your image path goes here...")) instead of tkinter.PhotoImage()

deateaterOG
  • 61
  • 1
  • 9
0
# in terminal pip install pillow

from tkinter import *
from PIL import Image, ImageTk

a_root = Tk()

# WINDOW SIZING

a_root.geometry("600x400")
a_root.minsize(400, 400)
a_root.maxsize(800, 800)

# for PNG IMG
# photo = PhotoImage(file="mark1.png")
# for JPG IMG

image = Image.open("mark.jpg")
photo = ImageTk.PhotoImage(image)

img_label = Label(image=photo)
img_label.pack()

a_root.mainloop()
Flair
  • 2,123
  • 1
  • 24
  • 39
  • 1
    As 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](/help/how-to-answer). – Community Dec 29 '21 at 15:30
  • those are not Python comments – Matiiss Dec 31 '21 at 00:04
0

I was getting the same error. You just have to use a PNG file instead of a jpeg/jpg. if you dont have a png version you can convert your picture here ---> https://cloudconvert.com/

from tkinter import *


root = Tk()

root.geometry("1920x1080")
photo = PhotoImage(file="Akatsuki.png")


label = Label(image=photo)
label.pack()

root.mainloop()
0

Your version of Tkinter doesn't support jpg. I fix it by installing python 3.10 which has built-in 8.6.11 Tcl/Tk. https://www.python.org/download/mac/tcltk/

amimibear
  • 21
  • 5
-1

Install the OpenCV packages for Python:

pip install opencv-python

Then try this code:

import cv2
Img = cv2.imread("xxxxx.png") 
cv2.imwrite("xxxxx.png",img) 
# Your code goes here!
bouteillebleu
  • 2,370
  • 20
  • 30
Martin
  • 1