Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loading an image

1,373 views
Skip to first unread message

Chae Min Ho

unread,
Feb 6, 1997, 3:00:00 AM2/6/97
to

Hello Python lovers..
I'm trying to load an image file with PIL like
the following:

from Tkinter import *
import Image, ImageTk

photo = ImageTk.PhotoImage(Image.open("London1.jpg"))
canvas.create_image(10, 10, anchor=NW, image=photo)

And this is the error message that I got.

Traceback (innermost last):
File "photo.py", line 4, in ?
photo = ImageTk.PhotoImage(Image.open("London1.jpg"))
File "C:\Py14\pil\Lib\ImageTk.py", line 36, in __init__
self.__tk = Tkinter.PhotoImage()
File "C:\Py14\Lib\tkinter\Tkinter.py", line 1593, in __init__
apply(Image.__init__, (self, 'photo', name, cnf), kw)
File "C:\Py14\Lib\tkinter\Tkinter.py", line 1561, in __init__
if not master: raise RuntimeError, 'Too early to create image'
RuntimeError: Too early to create image

Anyone can help on this problem?
I'll appreciate any comment or any other ways to load an image.
Thank you.


Minho

Steven D. Majewski

unread,
Feb 6, 1997, 3:00:00 AM2/6/97
to

On 6 Feb 1997, Chae Min Ho wrote:

> Hello Python lovers.. I'm trying to load an image file with PIL like
> the following:
>
> from Tkinter import *
> import Image, ImageTk
>
> photo = ImageTk.PhotoImage(Image.open("London1.jpg"))
> canvas.create_image(10, 10, anchor=NW, image=photo)
>
> And this is the error message that I got.
>

> if not master: raise RuntimeError, 'Too early to create image'


> RuntimeError: Too early to create image

I fell over what looks like the same problem with Tkinter/without PIL.
( It was the same error message. )
Python/Tkinter seems, for some reaso,n to be fussier than Wish/Tcl/Tk
about the order of some operations. When I made sure the canvas.pack()
method was called before create_image, the errors went away.

[ I've been working on a small sample GUI app in Tck/Tk, Python/Tkinter,
and Java/AWT for comparison. I hope I can finish and post them
Real Soon. ]


---| Steven D. Majewski (804-982-0831) <sd...@Virginia.EDU> |---
---| Department of Molecular Physiology and Biological Physics |---
---| University of Virginia Health Sciences Center |---
---| P.O. Box 10011 Charlottesville, VA 22906-0011 |---
By doing just a little every day, you can gradually
let the task completely overwhelm you.


Fredrik Lundh

unread,
Feb 9, 1997, 3:00:00 AM2/9/97
to

> RuntimeError: Too early to create image

This is a Tkinter error, not a PIL error. You must initialize Tkinter
(by creating a Tk root object) before you can create photoimages.
Try:

from Tkinter import *
import Image, ImageTk

root = Tk()

canvas = Canvas(root, height=100, width=100)

photo = ImageTk.PhotoImage(Image.open("London1.jpg"))
canvas.create_image(10, 10, anchor=NW, image=photo)

/F

Fredrik Lundh

unread,
Feb 9, 1997, 3:00:00 AM2/9/97
to

> Python/Tkinter seems, for some reaso,n to be fussier than Wish/Tcl/Tk
> about the order of some operations. When I made sure the canvas.pack()
> method was called before create_image, the errors went away.

But the reason here was that before you create the first Tk() object,
there's no Tcl interpreter available (wish obviously has an advantage
here ;-)

This is a general problem with Tkinter; since almost all operations in
there need a Tcl/Tk-context, there's some pretty ugly _default_root
magic in there to handle cases when you don't have a widget handy. An
alternative could perhaps be to create a Tcl context when the _tkinter
was first initialized, but that might probably cause unwanted side-
effects in other cases...

Cheers /F

Chae Min Ho

unread,
Feb 11, 1997, 3:00:00 AM2/11/97
to

Fredrik Lundh (Fredri...@ivab.se) wrote:

: > RuntimeError: Too early to create image

: /F


Well, I followd exactly what you recommended.
But this time I got this error.

Traceback (innermost last):
File "photo.py", line 7, in ?


photo = ImageTk.PhotoImage(Image.open("London1.jpg"))

File "C:\Py14\pil\Lib\ImageTk.py", line 39, in __init__
self.paste(image)
File "C:\Py14\pil\Lib\ImageTk.py", line 47, in paste
block = Image.core.new_block(self.__mode, self.__size)
File "C:\Py14\pil\Lib\Image.py", line 23, in __getattr__
raise ImportError, "The _imaging C module is not installed"
ImportError: The _imaging C module is not installed

I didn't delete any module.
Do you know why this occurs?
Thank you again for your precious advice.

Min Ho

Fredrik Lundh

unread,
Feb 11, 1997, 3:00:00 AM2/11/97
to

> ImportError: The _imaging C module is not installed

As in the previous case, the error message means exactly what it says.

> I didn't delete any module.

Was PIL properly installed in the first place? Is there an
_imagingmodule.so (or _imaging.dll) in the path? See the material
available at http://www.python.org/sigs/image-sig/Imaging.html for
more information.

Cheers /F

0 new messages