I wanted to do an image viewer for a photo catalog project. Here is the code:
import Image, ImageTk
from Tkinter import *
filename = "C:/Pictures/2006/2006.07.04/E clarus 1 crop.jpg"
class MyFrame(Frame):
def __init__(self, master):
Frame.__init__(self, master)
im = Image.open(filename) # <--- chokes!
im.resize(250,150)
self.image = Label(self, image=ImageTk.PhotoImage(im))
self.image.grid()
self.grid()
mainw = Tk()
mainw.frame = MyFrame(mainw)
mainw.mainloop()
Here is the error message:
Traceback (most recent call last):
File "C:\Python24\imager.py", line 19, in -toplevel-
mainw.frame = MyFrame(mainw)
File "C:\Python24\imager.py", line 10, in __init__
im = Image.open(filename)
AttributeError: class Image has no attribute 'open'
which of course is nonsense, since (a) class Image certainly does have an 'open' method, and (b) I can run the offending code in interactive mode and get a result.
What gives?
Thanks,
Jeff
P.S. To add to the mystery, here is the slightly modified code out of the Scripts directory that came with the PIL module. It works fine (of course!)
#
# The Python Imaging Library
# $Id: pilview.py 2134 2004-10-06 08:55:20Z fredrik $
#
from Tkinter import *
import Image, ImageTk
#
# an image viewer
class UI(Label):
def __init__(self, master, im):
if im.mode == "1":
# bitmap image
self.image = ImageTk.BitmapImage(im, foreground="white")
Label.__init__(self, master, image=self.image, bg="black", bd=0)
else:
# photo image
self.image = ImageTk.PhotoImage(im)
Label.__init__(self, master, image=self.image, bd=0)
#
# script interface
import sys
if not sys.argv[1:]:
## print "Syntax: python pilview.py imagefile"
## sys.exit(1)
filename = "C:/Pictures/2006/2006.07.04/E clarus 1 crop.jpg"
else:
filename = sys.argv[1]
root = Tk()
root.title(filename)
im = Image.open(filename) <--- !!! works!
UI(root, im).pack()
root.mainloop()