How to read image data into Tkinter Canvas?

Frederic Rentsch anthra.norell at bluewin.ch
Sat Mar 3 17:10:39 CET 2012


Hi,
	Familiarizing myself with Tkinter I'm stuck trying to fill a Canvas
with an image. I believe the class I need is PhotoImage rather than
BitmapImage. But I have no luck with either. The PhotoImage doc lists
available handlers for writing GIF and PPM files. It doesn't say
anything about reading. I would assume that any widely used image format
would activate the appropriate handler automatically. To work with
formats other than GIF and PPM the doc recommends to resort to the Image
module (PIL). This approach also failed.
	I did manage to read a GIF file into PhotoImage, which seems to let my
code off the hook. And I have made sure beyond any doubt that my image
files exist and open with PIL.
	I must be doing something wrong and will much appreciate any help. 

Frederic



---------------------------------------------------------------------


Here's what happens:


First trial with PhotoImage:

. . .
canvas = Canvas (root)
picture = PhotoImage (file = "/home/fr/temp/wandbild.bmp")
image = canvas.create_image (10, 10, anchor = NW, image = picture)
. . .

Traceback (most recent call last):
  File "tk2.py", line 23, in <module>
    picture = PhotoImage (file = "/home/fr/temp/wandbild.bmp")
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 3288, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 3244, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file
"/home/fr/temp/wandbild.bmp"


---------------------------------------------------------------------


Second trial with BitmapImage:

. . .
picture = BitmapImage (file = "/home/fr/temp/wandbild.bmp")
. . .

Traceback (most recent call last):
  File "tk2.py", line 22, in <module>
    picture = BitmapImage (file = "/home/fr/temp/wandbild.bmp")
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 3347, in __init__
    Image.__init__(self, 'bitmap', name, cnf, master, **kw)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 3244, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: format error in bitmap data


---------------------------------------------------------------------


Third trial with Image.open:

. . .
picture = Image.open ("/home/fr/temp/wandbild.bmp")
print picture
image = canvas.create_image (10, 10, anchor = NW, image = picture)
. . .

<JpegImagePlugin.JpegImageFile image mode=RGB size=963x616 at 0x9A4D84C>
Traceback (most recent call last):
  File "tk2.py", line 24, in <module>
    image = canvas.create_image (10, 10, anchor = NW, image = picture)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 2159, in
create_image
    return self._create('image', args, kw)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 2150, in _create
    *(args + self._options(cnf, kw))))
_tkinter.TclError: image "<BmpImagePlugin.BmpImageFile image mode=RGB
size=3933x2355 at 0x9A0D84C>" doesn't exist


---------------------------------------------------------------------


Same thing happens with JPG files. As I said, GIF works into PhotoImage,
but that's the only format I managed. 





More information about the Python-list mailing list