18

I know that this question has already been asked several times, but I still couldn't figure out the answer to my problem. I keep getting the same error and don't know how to solve it.

This is my code:

from Tkinter import *
from PIL import Image, ImageTk
import os

window = Tk()
i = Image.open(pathToImage) 
if os.path.isfile(pathToImage):

     print 'image exists'
else:   
     print 'image does not exits'

label=Label(window, image=i)
label.pack()
window.mainloop()

It says that the image exists at the indicated path, but I keep getting this error message:

Traceback (most recent call last):
  File "ImageTest.py", line 31, in <module>
    label=Label(window, image=i)
  File "C:\Users\username\Anaconda2\lib\lib-tk\Tkinter.py", line 2597, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\username\Anaconda2\lib\lib-tk\Tkinter.py", line 2096, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=640x480 at 0x36DF278>" doesn't exist

I could not figure out how to solve this problem. Any help would be appreciated!

anonymous
  • 409
  • 1
  • 4
  • 14

3 Answers3

25

You should use PhotoImage instance as image value. Also, you need to keep the reference of your image.

im = Image.open(pathToImage)
ph = ImageTk.PhotoImage(im)

label = Label(window, image=ph)
label.image=ph  #need to keep the reference of your image to avoid garbage collection
Lafexlos
  • 7,618
  • 5
  • 38
  • 53
  • Why does one need to keep the reference? Is it "consumed" after one use like "show(), configure(), etc."? I don't understand the mechanism. – Semo Oct 17 '17 at 12:37
  • 3
    @Semo [This effbot link](http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm) explains that concept better than I can. – Lafexlos Oct 17 '17 at 12:48
  • 1
    Looks like the effbot link is dead for some time. You can check [this blog post](https://blog.furas.pl/python-tkinter-why-label-doesnt-display-image-bug-with-garbage-collector-in-photoimage-GB.html) from [furas](https://stackoverflow.com/users/1832058/furas) which also contains a wayback machine of the effbot link above. – Lafexlos Aug 06 '21 at 10:38
15

A quick hacky fix is to provide the .PhotoImage with the correct master:

i = ImageTk.PhotoImage(pathToImage, master=window)
Guillaume G
  • 313
  • 1
  • 2
  • 15
Pedro Carlos
  • 151
  • 1
  • 2
1

It seems to be an Anaconda - Spyder - Iphyton problem. Solution is here.

Reciting this answer for future readers:

I struggled with the same problem, but only while using spyder (no problems using pydev in eclipse). I found for myself two solutions, that worked for me.

Solution 1: Goto Tools => Preferences and chose IPython console on the left. In the IPython console menu chose the Graphics Tab and disable the checkbox "Active support" at the top (Support for graphics). Save and restart you Kernel before you run the code

Solution 2: Goto Run => Configure and chose "Execute in an external System terminal", Save and run the code

As mentiond above, both solutions worked for my issue.

Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
andsa
  • 221
  • 2
  • 5