ImageTk.PhotoImage Crash

2019-06-17 03:00发布

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

3条回答
等我变得足够好
2楼-- · 2019-06-17 03:20

So this is an ancient question, but in case someone stumbles upon this (like I just did), the error message is from Tcl (tclExecute.c). I have no idea what's triggering it, but one thing worth trying is to create a Tk instance before calling PhotoImage:

root = Tk()
image = Image.open("0.gif")
photo = ImageTk.PhotoImage(image)
查看更多
劳资没心,怎么记你
3楼-- · 2019-06-17 03:38

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!

查看更多
家丑人穷心不美
4楼-- · 2019-06-17 03:41

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

查看更多
登录 后发表回答