[Image-SIG] PIL and Tk PhotoImage

Fredrik Lundh fredrik@pythonware.com
Tue, 16 Jan 2001 10:49:51 +0100


mark wrote:
> The following simple program is supposed to read an image
> using PIL and then convert it into a Tkinter PhotoImage (Running
> on a Mac):
> 
>    from Image import *
>    from Tkinter import *
> 
>    im = open("lena.jpg")
>    im.load()
>    tkIm = PhotoImage(im)

this won't work: you must use the PIL-aware PhotoImage class
from the ImageTk module, not the class with the same name
from Tkinter...

>        RuntimeError: Too early to create image
> 
> Why is it too early to create the image?

you need to create a Tk instance first (this initializes
the Tcl and Tk subsystems).

>    from Image import *
>    from Tkinter import *
+    import ImageTk
>
+    root = Tk()
+    root.withdraw() # no root window yet
> 
>    im = open("lena.jpg")
>    im.load() # not really necessary
>    tkIm = ImageTk.PhotoImage(im)

Hope this helps!

Cheers /F