-2

I´m new to python and have problems with tkinker and the Images.

My error is:

Traceback (most recent call last):
  File "D:/python/First Project/Weather app.py", line 12, in <module>
    background_image = tk.PhotoImage(file='landscape.jpg')
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3542, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 3498, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "landscape.jpg"

and the associated code is:

import tkinter as tk

root = tk.Tk()


background_image = tk.PhotoImage(file='D:\python\First Project\landscape.jpg')
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)


root.mainloop()

The ending of the file isn´t renamed and originally .jpg. I also tried to edit it with

background_image = ImageTk.PhotoImage(Image.open('your.png'))
Ben
  • 38
  • 1
  • 5
  • 1
    This question shows no sign of research. There are many questions on this site with that exact error message (except for the filename). – Bryan Oakley Sep 27 '20 at 14:39
  • @BryanOakley next time, please just vote to close duplicate questions as duplicates rather than complaining. – Karl Knechtel Feb 22 '23 at 21:22
  • @KarlKnechtel: I wasn't complaining, I was pointing out why the OP might expect to get downvotes. But, you're right, I should have closed it as a duplicate. – Bryan Oakley Feb 22 '23 at 22:12

1 Answers1

1

The PhotoImage method does not appear to support JPG files. I received the same error (couldn't recognise data in "image.jpg") when using a JPG file. When using a PNG file I did not receive any such errors.

Note that you cannot just change the file extension of a JPG file to turn it into a PNG file, because the data in PNG files is different from a JPG file. You will need to convert your JPG file to a PNG (or any other image file type supported by the PhotoImage method.)