[Image-SIG] Display Image from ImageDraw

Fredrik Lundh fredrik@pythonware.com
Wed, 31 Oct 2001 20:40:09 +0100


Richard Townsend wrote:
> 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.

> TclError: image "<Image.Image instance at 0081929C>" doesn't exist

to register the image with Tk, you must wrap it in an
ImageTk.PhotoImage object.

    from Tkinter import *
    import Image, ImageDraw, ImageTk
    root = Tk()
    img = Image.new("RGB", (100, 100))
    draw = ImageDraw.Draw(img)
    draw.rectangle((10,10,90,90), outline=(255,255,255))
    del draw
>   photo = ImageTk.PhotoImage(img)
>   label = Label(root, image=photo)
>   label.photo = photo # keep a reference
    label.pack()
    root.mainloop()

regards /F