1

I am trying to display a PGM image in Tkinter window, however I keep getting the error of "couldn't recognize data in image file". Is there something wrong with my code??

import tkinter as tk
from tkinter import filedialog
from tkinter import *
from tkinter import PhotoImage
def browse_file(self):
    path = filedialog.askopenfilename()
    if len(path) > 0:
        photo = tk.PhotoImage(file=path)
        cv = tk.Canvas()
        cv.pack(side='top', fill='both', expand='yes')
        cv.create_image(10, 10, image=photo, anchor='nw')
  • Are you getting the error in `cv.create_image()` function call, or somewhere else in the code? Have you tried with different image files perhaps the image is corrupt in some way -- try other image formats and see if they work. – RyanNerd Feb 06 '19 at 02:33
  • hi, i did try different pgm files,they all say “couldn't recognize data in image file” –  Feb 06 '19 at 02:51
  • It may be that Tkinker does not support PGM files. This looks like a possible dupe of: https://stackoverflow.com/questions/47357090/tkinter-error-couldnt-recognize-data-in-image-file – RyanNerd Feb 06 '19 at 02:54
  • Possible duplicate of [Tkinter error: Couldn't recognize data in image file](https://stackoverflow.com/questions/47357090/tkinter-error-couldnt-recognize-data-in-image-file) – stovfl Feb 06 '19 at 07:57
  • Hi, it's true that tkinter PhotoImage doesn't accept png and jpeg images - it is only meant to read GIF PGM and PPM images. that's why I am confused as to why it is not reading it. –  Feb 07 '19 at 14:18

0 Answers0