7

So I am running python 2.6 on a macbook pro and trying to write the code in python to display an image from a file in a label on a tkinter gui. The image is called image.png. The program runs without errors when I use this code

i = Image.open("image.png")

but when I do this code (I add one line):

i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)

The program will crash and say "Bus error" in the command line. I don't even know what that means. I would think that PIL is installed correctly, since Image works, but the fact that ImageTk does not work puzzles me. Can anybody tell me what might be causing this Bus error?

EDIT: Well I made a new program to test the error further. Here is the exact script I ran:

import Image
import ImageTk

i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)

Now instead of getting "Bus error", this is my traceback.

Traceback (most recent call last):
  File "imageTest.py", line 5, in <module>
    photo = ImageTk.PhotoImage(i)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/ImageTk.py", line 113, in __init__
    self.__photo = apply(Tkinter.PhotoImage, (), kw)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3285, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3226, in __init__
    raise RuntimeError, 'Too early to create image'
RuntimeError: Too early to create image
Exception AttributeError: "PhotoImage instance has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <ImageTk.PhotoImage instance at 0x3c7a30>> ignored
perimosocordiae
  • 17,287
  • 14
  • 60
  • 76
Dylan
  • 949
  • 3
  • 13
  • 23

3 Answers3

7

I don't know about the Bus Error, but you need to create a Tk window before you can call PhotoImage. This script works for me-

import Image 
import ImageTk
from Tkinter import Tk

window = Tk()
i = Image.open("image.png") 
photo = ImageTk.PhotoImage(i)
Rob Lourens
  • 15,081
  • 5
  • 76
  • 91
  • Okay, when I run this code it makes like a GUI is about to open, then a popup comes up with the words, "Python quit unexpectedly", and the terminal window prints "Bus error" – Dylan Jul 07 '10 at 01:34
  • This post may be relevant-http://bytes.com/topic/python/answers/532341-tkinter-bus-error-right-away Which version of OSX are you running? The code above worked for me in Python 2.6 and OS 10.6 – Rob Lourens Jul 07 '10 at 13:07
  • I'm running 10.6 and Python 2.6 also. That post looks like it might be useful though, so I will look at it. Thanks – Dylan Jul 08 '10 at 02:17
  • Okay, well I think I found what would be the solution to my problem, if I knew how to go about doing it. I felt like there was so much content that this warranted a second question, so could you please look here and tell me if you know how to do what it is telling me to? http://stackoverflow.com/questions/3200308/how-to-add-tkinter-support-for-pil-python-library – Dylan Jul 08 '10 at 02:28
6

ImageTk.PhotoImage has a garbage collection (ref count) bug in it. You must place a reference to the PhotoImage object in either a global variable of a class instance variable (e.g., self.myphoto = ImageTk.PhotoImage(i)).

See this warning:

http://infohost.nmt.edu/tcc/help/pubs/pil/image-tk.html

mckoss
  • 6,764
  • 6
  • 33
  • 31
  • Try doing both this and the [currently accepted answer](https://stackoverflow.com/a/3183821/2573061). I experienced this problem, and both were required to fix it. – C8H10N4O2 Jan 05 '18 at 17:43
2

Even thought you do need to call a Tk window you also need to set the directory so that it can find the image.png.

import os
import Image 
import ImageTk
from Tkinter import Tk

os.chdir('C:/../../') # put file path for the image.

window = Tk()
i = Image.open("image.png") 
photo = ImageTk.PhotoImage(i)

window.mainloop()
jcham55
  • 21
  • 1