Tkinter 和 PIL 未加载 jpeg

Eva*_*ark 3 python tkinter python-imaging-library

我试图让我的 Tkinter 程序从标准 .gif 加载和显示 .jpgs 文件

import Image,ImageTk
root = Tk()
PILFile = Image.open("Image.jpg")
Image = PhotoImage(file=PILFile)
ImageLabel = Label(root,image=Image)
ImageLabel.image = Image
ImageLabel.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

我收到的错误消息如下:

import Image,ImageTk
root = Tk()
PILFile = Image.open("Image.jpg")
Image = PhotoImage(file=PILFile)
ImageLabel = Label(root,image=Image)
ImageLabel.image = Image
ImageLabel.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)

我绝对确定该文件以正确的格式存在,我可能做错了什么?

fal*_*tru 5

根据Tkinter PhotoImage 类

PhotoImage 类可以从文件中读取 GIF 和 PGM/PPM 图像:

.... 如果您需要处理其他文件格式,Python 图像库 (PIL) 包含的类可让您加载 30 多种格式的图像,并将它们转换为与 Tkinter 兼容的图像对象:

from PIL import Image, ImageTk

image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)
Run Code Online (Sandbox Code Playgroud)

-> 替换Tkinter.PhotoImageImageTk.PhotoImage

root = Tk()
PILFile = Image.open("Image.jpg")
Image = ImageTk.PhotoImage(PILFile) # <---
ImageLabel = Label(root, image=Image)
ImageLabel.image = Image
ImageLabel.pack()
root.mainloop()
Run Code Online (Sandbox Code Playgroud)