[Image-SIG] Display Image from ImageDraw

Richard Townsend richard@starfighter.freeuk.com
Wed, 31 Oct 2001 18:50:07 -0000


I would like to generate an image directly from data and then display that
image on a Tkinter Canvas or Label. When I try to do this, I get an error
saying the image doesn't exist. However I can save that image to a file and
display it Paint Shop Pro and xv.

Here is a simplified example:

from Tkinter import *
import Image, ImageDraw
root = Tk()
img = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(img)
draw.rectangle((10,10,90,90), outline=(255,255,255))
del draw
label = Label(root, image=img)
label.pack()
root.mainloop()

Here's the error:

D:\Development\Python\exp\exp\pil>python pil_draw.py
Traceback (most recent call last):
  File "pil_draw.py", line 14, in ?
    label = Label(root, image=img)
  File "c:\python21\lib\lib-tk\Tkinter.py", line 2261, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "c:\python21\lib\lib-tk\Tkinter.py", line 1756, in __init__
    self.tk.call(
TclError: image "<Image.Image instance at 0081929C>" doesn't exist


I am getting similar errors on Win98 and Linux.

Is there a way to do this ?

regards
Richard Townsend