Tkinter和PIL没有加载jpegs

时间:2014-01-11 14:40:26

标签: python tkinter python-imaging-library

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

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]

我绝对确定文件格式正确,我可能做错了什么?

2 个答案:

答案 0 :(得分:4)

根据The Tkinter PhotoImage Class

  

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

     

....   如果您需要使用其他文件格式,Python Imaging Library(PIL)包含的类允许您以超过30种格式加载图像,并将它们转换为与Tkinter兼容的图像对象:

from PIL import Image, ImageTk

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

- &GT;将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()

答案 1 :(得分:1)

由于参数file

而发生错误
Image = PhotoImage(file=PILFile)

这将指定文件路径。此外,您需要ImageTk.PhotoImage。相反,你想要:

Image = ImageTk.PhotoImage(PILFile)