Uploading pictures python tkinter

Viewed 189

So I am completely new to Python and I'm trying to learn how to build GUI's for my business to help automate some of my work. I'm trying to upload a jpeg to the app I'm making, but I keep getting the same error code. Here it is along with my code beneath it:

    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "C:\Users\samuel baker\Desktop\Yo3Updated140_Orthomosaic_export_SunJun14164005.937211.jpg

Code:

from tkinter import *
from PIL import ImageTk, Image

image = ImageTk, Image
root = Tk()

myCanvas = Canvas(root, height=700, width=700, bg="skyblue")
myCanvas.pack()

myFrame = Frame(root, height=450, width=450, bg="pink")
myFrame.place(relx=0.5, rely=0.5, anchor=CENTER)

my_Image = PhotoImage(image, file="C:\\Users\\samuel baker\\Desktop\\Yo3Updated140_Orthomosaic_export_SunJun14164005.937211.jpg")
Canvas.create_image(0, 0, anchor=NW, image=my_Image)

myLabel = Label(root, text="Hi Sam", fg="gold", bg="White", padx="25", pady="20", font=('Courier New', 30))
myLabel.place(relx=0.5, rely=0.1, anchor=CENTER)

root.mainloop()
2 Answers

You used Python built-in PhotoImage() which does not support JPEG image.

Change

my_Image = PhotoImage(image, file=...)

to

my_Image = ImageTk.PhotoImage(file=...)`

Also the following line

Canvas.create_image(0, 0, anchor=NW, image=my_Image)

should be

myCanvas.create_image(0, 0, anchor=NW, image=my_Image)

PS: I think the line image = Image, ImageTk is a typo and so remove it.

Python's built in PhotoImage function cannot import a JPEG image. Try using

my_Image = ImageTk.PhotoImage(•••)

Instead of

my_Image = PhotoImage(•••)
Related