1

I'm creating a GUI for my program, and I'm attempting to display a user selected ppm image file. When using the Image.open() method on a selected ppm file I get this error

    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'

The meat of my program requires that images use the P3 format, but I can't find anywhere in the official PIL documentation that mentions which PPM formats it supports, just that it supports PPM.

Here's the code for the 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()
  • According to the answers to this question, PIL doesn't support the P3 version: https://stackoverflow.com/q/4101576/7432. In fact, this question should probably be closed as a duplicate of that older question. – Bryan Oakley May 20 '20 at 23:39
  • @BryanOakley You're right. Now that I've been looking around more I'll likely just change my code to function with jpegs. – IDEwhatyoumean May 21 '20 at 01:22

0 Answers0