gpt4 book ai didi

python - Tkinter 和 PIL 不加载 jpeg

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:40 24 4
gpt4 key购买 nike

我试图让我的 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()

我得到的错误信息如下:

Traceback (most recent call last):
File "/home/paragon/Programs/Python/Web/MP3Tagger.py", line 25, in <module>
albumart = PhotoImage(file=PILfile)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 3271, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 3227, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "<JpegImagePlugin.JpegImageFile image mode=RGB size=1500x1500 at 0x98AEFCC>": no such file or directory
[Finished in 0.4s with exit code 1]

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

最佳答案

根据 The Tkinter PhotoImage Class :

The PhotoImage class can read GIF and PGM/PPM images from files:

.... If you need to work with other file formats, the Python Imaging Library (PIL) contains classes that lets you load images in over 30 formats, and convert them to Tkinter-compatible image objects:

from PIL import Image, ImageTk

image = Image.open("lenna.jpg")
photo = ImageTk.PhotoImage(image)

-> 将 Tkinter.PhotoImage 替换为 ImageTk.PhotoImage:

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

关于python - Tkinter 和 PIL 不加载 jpeg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21063576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com