python - PIL 无法读取 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__
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()
解决方案
推荐阅读
- javascript - 我创建了 3 个按钮,如果您单击任何按钮,网站的语言会更改我的 javascript 代码在本地主机上有效,但在在线服务器上无效
- python - Python:虚拟还是存根?
- c# - 如何在 c# 中的浮点列表中添加浮点列表,统一?
- facebook - Facebook API 离线转化匹配问题
- http-put - 内联组件未定义或不允许 (HTTP PUT)
- angular - 如何从组件的子元素中使用指令方法?
- python-3.x - Python Key 错误 - 尝试使用语句时
- python - Python Selenium 多线程将每个线程的值加在一起
- python - 在 python 中向 sqlite3 数据库添加按钮文本
- vue.js - 在具有多个组件的 Vue 中单击事件作为父级