1

プログラムの 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 形式を使用することを必要としますが、PPM をサポートしているというだけで、どの PPM 形式がサポートされているかについて言及している公式の PIL ドキュメントのどこにも見つかりません。

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()
4

0 に答える 0