Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageTk.PhotoImage Crash

I have been trying to resize images using PIL then display them using Tkinter, but the program has been crashing and I've isolated the problem to the second line below:

image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)

And this is my imports:

from Tkinter import * 
from PIL import Image, ImageTk 

I've read around that Tk must be initialized and I do this in the program before it reaches those lines in the program. So I don't know what it is.

I am running OSX and python 2.7 interpreter on eclipse (using PyDev).

UPDATE:

Error message on eclipse says:

STACK: Stack after current is in use
like image 990
Victor Mota Avatar asked May 27 '11 02:05

Victor Mota


2 Answers

I've seen that error before using tkinter. I think it had something to do with an older version of tkinter. I updated my python version and tkinter version and it went away. Does this error happen when you run your code on a different OS/Computer/Platform/Version of Python? What version of tkinter are you using? Some google searching revealed these two pages which describe the same bug while using tkinter...

http://osdir.com/ml/python.leo.general/2008-03/msg00060.html
http://fornax.phys.unm.edu/lwa/trac/ticket/3

I can't see all your code, but I'm betting that there is not necessarily anything wrong with your code. The following code worked for me...

from Tkinter import * 
from PIL import Image, ImageTk 

# resize image with PIL
im = Image.open('path to gif')
resized_im = im.resize((400,400,),Image.ANTIALIAS)

# display image in tkinter window
window = Tk()
tk_im = ImageTk.PhotoImage(resized_im)
window.geometry('%dx%d' % (resized_im.size[0],resized_im.size[1]))
label_image = Label(window, image=tk_im)
label_image.place(x=0,y=0,width=resized_im.size[0],height=resized_im.size[1])
window.mainloop()

Using....
ubuntu 10.04 64 bit
python 2.6.5
python-imaging-tk 1.1.7
python-tk 2.6.5 (which uses version 8.5.0 of tkinter)
python imaging library (PIL) 1.1.7
eclipse 3.7.1
pydev 2.5.0.2012050419

Good luck!

like image 153
b10hazard Avatar answered Sep 23 '22 03:09

b10hazard


I've been using both Tk, PIL and resizing images for a current project and the following code works fine for me.

#Imports
from Tkinter import * 
from PIL import Image, ImageTk 

#Create Tk instance
root = Tk()

#Open image and resize
image = Image.open("path/to/image/file").resize((400,400), Image.ANTIALIAS)
photo = ImageTk.PhotoImage(image)

After that, I find it easiest to display the images as labels in tkinter like so.

image_label = Label(root, width = 400, height = 400, image = photo bd = 0)

(I like the bd = 0 as otherwise I get a thin white border around the image.) Hope this has helped you. Good luck! Ed

like image 39
Music Champ29 Avatar answered Sep 22 '22 03:09

Music Champ29