我正在为程序创建GUI,并试图显示用户选择的ppm图像文件。在选定的ppm文件上使用Image.open()方法时,出现此错误
Exception in Tkinter callback
Traceback (most recent call last):
File "/home/owner/anaconda3/envs/proj1/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
return self.func(*args)
File "/home/owner/PycharmProjects/Project1/gui.py", line 35, in fileDialog
photo = Image.open(self.filename)
File "/home/owner/anaconda3/envs/proj1/lib/python3.7/site-packages/PIL/Image.py", line 2896, in open
"cannot identify image file %r" % (filename if filename else fp)
PIL.UnidentifiedImageError: cannot identify image file '/home/owner/PycharmProjects/Project1/input_images/Platonic_figure_at_UMN-tiny.ppm'
我的程序的要求是图像必须使用P3格式,但是我在官方的PIL文档中找不到任何地方提及该支持的PPM格式,只是它支持PPM。
这是GUI的代码:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import Image
from k_means import *
from image_utils import *
class Root(Tk):
def __init__(self):
super(Root, self).__init__()
self.title("K-means Visualization Tool")
self.minsize(1000, 700)
self.imgFrame = ttk.Labelframe(self)
self.imgFrame.grid(column=0, row=3, padx=20, pady=40)
self.labelFrame = ttk.LabelFrame(self, text="Open File")
self.labelFrame.grid(column=0, row=1, padx=20, pady=20)
self.button()
def button(self):
self.button = ttk.Button(self.labelFrame, text="Browse A File", command=self.fileDialog)
self.button.grid(column=1, row=1)
def fileDialog(self):
self.filename = filedialog.askopenfilename(initialdir="/", title="Select A File", filetypes=
(("PPM P3", "*.ppm"), ("all files", "*.*")))
self.label = ttk.Label(self.labelFrame, text="")
self.label.grid(column=1, row=2)
self.label.configure(text=self.filename)
photo = Image.open(self.filename)
input_img = PhotoImage(photo)
self.imgLabel = ttk.Button(self.imgFrame, image=input_img)
root = Root()
root.mainloop()