[Tutor] Bitmaps and TK

Magnus Lycka magnus@thinkware.se
Wed Dec 4 12:04:22 2002


At 15:32 2002-12-04 +0000, Ian Dickinson wrote:
>I am farly new to this Python / Tk thing

Welcome. I hope the frustration will cease soon.

>Image.open does not appear to exist

I'm not sure if you refer to PIL's Image module or Tkinters Image
class here. Tkinter's Image doesn't have any open method. It's a
base class, and I think you always use subclasses. I use PIL in the
packaging provided by ReportLab, and there I would do

import PIL.Image

i = PIL.Image.open("C:\\Python_projects\\CRAP\\topleftcorner.bmp")

to open an image with PIL. This has nothing to do
with Tkinter though. Maybe you are mixing things up?

Tkinter.Image is a base class for BitmapImage and PhotoImage
as far as I know. You wouldn't use that.

PIL.Image is a more generic module used for image processing
etc. You can load an image using

"import Image" does nothing for me. (ImportError: No module named Image)

Perhaps you can tell more exactly what part of the
documentation you find confusing.

 >>> import PIL.Image
 >>> help(PIL.Image)

and

 >>> import Tkinter.Image
 >>> help(Tkinter.Image)

will tell you what you will find in the Tkinter.Image class
and the PIL.Image module.

I think you could do something like this:

# I'll avoid "from X import *" so I don't mix things up...
import PIL.Image, PIL.ImageTk
import Tkinter

im = PIL.Image.open(r'G:\WINNT\Bubblor.bmp')

root = Tkinter.Tk()

photo = PIL.ImageTk.PhotoImage(im)

l = Tkinter.Label(image = photo)
l.pack()

root.mainloop()


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se