1

Every time, even though I have the source file, the same error message appears - 'Tcl error...could not recognize data in image file'. The problem also occurs with other images present in my disk drive.

Here is the code:

from tkinter import *
window=Tk()
window.geometry("420x420")
window.title('My first GUI programme')
source='D:\\Desktop\\op.jfif'
icon=PhotoImage(file=source)
window.iconphoto(True, icon)
window.mainloop()

Here goes the error:

PS D:\Documents\pythonProject> & "C:/Program Files (x86)/Python/python.exe" "d:/Documents/pythonProject/Learning python/Practice.py"
Traceback (most recent call last):
  File "d:\Documents\pythonProject\Learning python\Practice.py", line 6, in <module>  
    icon=PhotoImage(file=source)
  File "C:\Program Files (x86)\Python\lib\tkinter\__init__.py", line 4064, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Program Files (x86)\Python\lib\tkinter\__init__.py", line 4009, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "D:\Desktop\op.jfif"

I don't know if there is any problem with my code or i need to change any settings. I would be thankful to you if you provide me any assistance.

5
  • 1
    Python Tkinter supports GIF, PGM, PPM, and PNG. So, try changing the extension of the file to one of them Commented Sep 4, 2021 at 7:19
  • you can also try using Image and ImageTk from pillow library (PIL), also don't use * when importing Commented Sep 4, 2021 at 8:34
  • A JFIF file is a bitmap graphic that uses JPEG compression. Try importing PIL to view it. Commented Sep 4, 2021 at 8:34
  • @PCM: just changing the extension doesn't do anything but change the name. It's the contents of the file that need to be changed. Commented Sep 4, 2021 at 15:37
  • @BryanOakley Yes you are right, i had to convert my image to a png file by changing its data through an online converter, and it, then, worked for me, thx for your support Commented Sep 5, 2021 at 4:14

2 Answers 2

2

I managed to do this by changing the file extension and file content of my image file to 'png' which worked in python.

Sign up to request clarification or add additional context in comments.

Comments

1

Python Tkinter supports GIF, PGM, PPM, and PNG. So, try changing the extension of the file to one of them

i see your problem

i also have the same problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.