Tkinter.PhotoImage doesn't support png image
I am using Tkinter to write a GUI and want to render a png file in Tkiner.Label
. So I have a code like this:
self.vcode.img = PhotoImage(data=open('test.png').read(), format='png')
self.vcode.config(image=self.vcode.img)
This code works correctly on my Linux machine . But when I run it on my windows computer it fails. I also tested on several other machines (including Windows and Linux), it was failing all the time.
Traceback is:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__ return self.func(*args) File "C:\Documents and Settings\St\client\GUI.py", line 150, in showrbox SignupBox(self, self.server) File "C:\Documents and Settings\St\client\GUI.py", line 197, in __init__ self.refresh_vcode() File "C:\Documents and Settings\St\client\GUI.py", line 203, in refresh_vcode self.vcode.img = PhotoImage(data=open('test.png').read(), format='png') File "C:\Python27\lib\lib-tk\Tkinter.py", line 3323, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Python27\lib\lib-tk\Tkinter.py", line 3279, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) TclError: image format "png" is not supported
If I remove format='png'
in the original code, the trace becomes:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Python27\lib\lib-tk\Tkinter.py", line 1486, in __call__ return self.func(*args) File "C:\Documents and Settings\St\client\GUI.py", line 150, in showrbox SignupBox(self, self.server) File "C:\Documents and Settings\St\client\GUI.py", line 197, in __init__ self.refresh_vcode() File "C:\Documents and Settings\St\client\GUI.py", line 203, in refresh_vcode self.vcode.img = PhotoImage(data=open('test.png').read()) File "C:\Python27\lib\lib-tk\Tkinter.py", line 3323, in __init__ Image.__init__(self, 'photo', name, cnf, master, **kw) File "C:\Python27\lib\lib-tk\Tkinter.py", line 3279, in __init__ self.tk.call(('image', 'create', imgtype, name,) + options) TclError: couldn't recognize image data
So what should I do to make it support png files?
source to share
tkinter only supports 3 bat file formats which are GIF, PGM and PPM. You will need to either convert the files to .GIF or load them (much easier, but as jonrsharpe said, nothing will work without converting the file in the first place), or you can port your program to Python 2.7 and use the Python Imaging Library ( PIL) and its tkinter extension to use a PNG image.
Link you might find useful: http://effbot.org/tkinterbook/photoimage.htm
source to share
PIL is now replaced with pillow http://pillow.readthedocs.io/en/3.2.x/
Decision:
from Tkinter import *
import PIL.Image
import PIL.ImageTk
root = Toplevel()
im = PIL.Image.open("photo.png")
photo = PIL.ImageTk.PhotoImage(im)
label = Label(root, image=photo)
label.image = photo # keep a reference!
label.pack()
root.mainloop()
If PIL
not found in the code, you need to install pillow
:
pip install pillow
source to share
Tkinter 8.6 supports the png file format, but tkinter 8.5 does not. If you have the update python option and you should be fine to use png. If you need to use an older version of python you should use Pillow (fork supported) which also works in python3.
If you're starting a new project , don't use python2 or PIL as suggested in the accepted answer, they are both discounted technologies.
source to share
try with PIL library instead of converting your image to GIF, PGM or PPM (PhotoImage), only accept these 3 formats.
import tkinter as tk
import PIL.Image
import PIL.ImageTk
base = tk.Tk()
base.title("Dialy Dose")
logoPath = r"C:\Users\saigopi\Downloads\logo.png"
ref = PIL.Image.open(logoPath)
photo = PIL.ImageTk.PhotoImage(im)
inputEdit = tk.Label(base,text="Enter Quote")
save = tk.Button(base,text="Save",background="green",command=save())
logo = tk.Label(base,image=photo,text="Logo bro lite")
quote = tk.Label(base,text="I am saying you are more than something")
inputEdit.pack()
save.pack()
logo.pack()
quote.pack()
base.mainloop()
source to share
Fixed in the official python.org 64-bit (only) installer for OS X. Tk version 8.6 is included. Warning: if you are using homebrew it brew install python3
will only give you 8.5 at the time of posting , while png requires 8.6 to use, so you will have to use the official installer instead. To check which Tk you are using:
$ python3 -c 'import tkinter; print(tkinter.TkVersion);'
If it says 8.6, you can go.
source to share