tkinker error: couldn´t recognize data in image file

tkinker error: couldn´t recognize data in image file

我是 python 的新手,对 tkinker 和图像有疑问。

我的错误是:

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"

相关代码是:

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()

文件的结尾未重命名,最初为 .jpg。 我也尝试用

编辑它
background_image = ImageTk.PhotoImage(Image.open('your.png'))

PhotoImage 方法似乎不支持 JPG 文件。我在使用 JPG 文件时收到相同的错误 (couldn't recognise data in "image.jpg")。使用 PNG 文件时,我没有收到任何此类错误。

请注意,您不能只更改 JPG 文件的文件扩展名以将其转换为 PNG 文件,因为 PNG 文件中的数据与 JPG 文件不同。您需要将 JPG 文件转换为 PNG(或 PhotoImage 方法支持的任何其他图像文件类型。)