0

i'm trying to insert an image using Tkinter but it doesn't work, there is an error message saying : (In fact it is saying that python can't recognise the data in image file)

Traceback (most recent call last):
  File "E:/Tle/ISN/Programs (Pyhton)/IMC (Widget) ULTIMATE.py", line 10, in <module>
    my_image = PhotoImage(file="C:/Users/mateo.PCMATEO/Pictures/MonCoachPersonnel.jpg")
  File "C:\Users\mateo.PCMATEO\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3545, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\mateo.PCMATEO\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 3501, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "C:/Users/mateo.PCMATEO/Pictures/MonCoachPersonnel.jpg"

And here's the code i've entered :

from tkinter import*
import tkinter as tk
window = tk.Tk()
window.title("My Personnal Fitness Coach")
window.geometry("400x500")
window.configure(background='grey')

canvas = Canvas(window, width = 100, height = 100)
canvas.pack
my_image = PhotoImage(file="C:/Users/mateo.PCMATEO/Pictures/MonCoachPersonnel.jpg")
canvas.create_image(0, 0, anchor = NW, image=my_image)

window.mainloop()

The problem is that i don't have any modules except the ones predownloaded with python and i don't want to install some yet. So could you help me?

stovfl
  • 14,998
  • 7
  • 24
  • 51
  • tk's PhotoImage class doesn't support jpg. – Bryan Oakley Sep 24 '19 at 18:01
  • Possible duplicate of [Tkinter error: Couldn't recognize data in image file](https://stackoverflow.com/questions/47357090/tkinter-error-couldnt-recognize-data-in-image-file) – stovfl Sep 24 '19 at 18:09

2 Answers2

0

if you use jpg you must do so

from PIL import ImageTk

then

my_image = ImageTk.PhotoImage(file="MonCoachPersonnel.jpg")
canvas.create_image(50, 50, image=my_image, anchor=NW)

I've simplify the file dir, note that I've increase the dimension of the pic, from 0,0 to 50,50.

1966bc
  • 1,148
  • 1
  • 6
  • 11
  • In fact i've already tried something like that but it says that i don't have any PIL module.. so that's why i don't unuderstand – Rezestick Sep 24 '19 at 18:52
0

Please, try this (only change image file path)

from tkinter import*
import tkinter as tk
from PIL import ImageTk, Image


window = tk.Tk()
window.title("My Personnal Fitness Coach")
window.geometry("400x500")
window.configure(background='grey')

canvas = Canvas(window, width = 100, height = 100)
canvas.pack()

photo_image = ImageTk.PhotoImage(Image.open('xxxx\\racecar.jpg'))
my_image = canvas.create_image(0, 0, image=photo_image, anchor=NW) 

# my_image = PhotoImage(file="xxxx\\racecar.jpg")
# canvas.create_image(0, 0, anchor = NW, image=my_image)

window.mainloop()
Mr.Llaka
  • 36
  • 2