Python PIL在读取PPM图像文件时遇到问题
我正在为我的程序创建一个GUI,并试图显示一个用户选择的ppm图像文件。在对选定的ppm文件使用Image.open()方法时,我会遇到此错误Python PIL在读取PPM图像文件时遇到问题,python,user-interface,tkinter,python-imaging-library,ppm,Python,User Interface,Tkinter,Python Imaging Library,Ppm,我正在为我的程序创建一个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__ retur
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()
根据对这个问题的回答,PIL不支持P3版本:。事实上,这个问题可能应该作为旧问题的重复来结束。@BryanOakley你说得对。现在,我已经四处查看了更多信息,我可能只会将代码更改为使用JPEG。根据对这个问题的回答,PIL不支持P3版本:。事实上,这个问题可能应该作为旧问题的重复来结束。@BryanOakley你说得对。现在,我已经环顾了更多的地方,我很可能只是改变我的代码,使其能够与JPEG一起工作。