2
# The Python Imaging Library.
5
# a Tk display interface
9
# 96-09-06 fl Added getimage method
10
# 96-11-01 fl Rewritten, removed image attribute and crop method
11
# 97-05-09 fl Use PyImagingPaste method instead of image type
12
# 97-05-12 fl Minor tweaks to match the IFUNC95 interface
13
# 97-05-17 fl Support the "pilbitmap" booster patch
14
# 97-06-05 fl Added file= and data= argument to image constructors
15
# 98-03-09 fl Added width and height methods to Image classes
16
# 98-07-02 fl Use default mode for "P" images without palette attribute
17
# 98-07-02 fl Explicitly destroy Tkinter image objects
18
# 99-07-24 fl Support multiple Tk interpreters (from Greg Couch)
19
# 99-07-26 fl Automatically hook into Tkinter (if possible)
20
# 99-08-15 fl Hook uses _imagingtk instead of _imaging
22
# Copyright (c) 1997-1999 by Secret Labs AB
23
# Copyright (c) 1996-1997 by Fredrik Lundh
25
# See the README file for information on usage and redistribution.
31
# The <b>ImageTk</b> module contains support to create and modify
32
# Tkinter <b>BitmapImage</b> and <b>PhotoImage</b> objects.
34
# For examples, see the demo programs in the <i>Scripts</i>
38
# --------------------------------------------------------------------
39
# Check for Tkinter interface hooks
43
def _pilbitmap_check():
45
if _pilbitmap_ok is None:
47
im = Image.new("1", (1,1))
48
Tkinter.BitmapImage(data="PIL:%d" % im.im.id)
50
except Tkinter.TclError:
54
# --------------------------------------------------------------------
58
# Creates a Tkinter-compatible photo image. This can be used
59
# everywhere Tkinter expects an image object. If the image is an RGBA
60
# image, pixels having alpha 0 are treated as transparent.
65
# Create a photo image object. The constructor takes either
66
# a PIL image, or a mode and a size. Alternatively, you can
67
# use the <b>file</b> or <b>data</b> options to initialize
68
# the photo image object.
70
# @def __init__(image=None, size=None, **options)
71
# @param image Either a PIL image, or a mode string. If a
72
# mode string is used, a size must also be given.
73
# @param size If the first argument is a mode string, this
74
# defines the size of the image.
75
# @keyparam file A filename to load the image from (using
77
# @keyparam data An 8-bit string containing image data (as
78
# loaded from an image file).
80
def __init__(self, image=None, size=None, **kw):
82
# Tk compatibility: file or data
84
if kw.has_key("file"):
85
image = Image.open(kw["file"])
87
elif kw.has_key("data"):
88
from StringIO import StringIO
89
image = Image.open(StringIO(kw["data"]))
92
if hasattr(image, "mode") and hasattr(image, "size"):
93
# got an image instead of a mode
99
mode = image.palette.mode
100
except AttributeError:
101
mode = "RGB" # default
103
kw["width"], kw["height"] = size
108
if mode not in ["1", "L", "RGB", "RGBA"]:
109
mode = Image.getmodebase(mode)
113
self.__photo = apply(Tkinter.PhotoImage, (), kw)
114
self.tk = self.__photo.tk
119
name = self.__photo.name
120
self.__photo.name = None
122
self.__photo.tk.call("image", "delete", name)
124
pass # ignore internal errors
127
# Get the Tkinter photo image identifier. This method is
128
# automatically called by Tkinter whenever a PhotoImage object is
129
# passed to a Tkinter method.
131
# @return A Tkinter photo image identifier (a string).
134
return str(self.__photo)
137
# Get the width of the image.
139
# @return The width, in pixels.
142
return self.__size[0]
145
# Get the height of the image.
147
# @return The height, in pixels.
150
return self.__size[1]
153
# Paste a PIL image into the photo image. Note that this can
154
# be very slow if the photo image is displayed.
156
# @param im A PIL image. The size must match the target region.
157
# If the mode does not match, the image is converted to the
158
# mode of the bitmap image.
159
# @param box A 4-tuple defining the left, upper, right, and
160
# lower pixel coordinate. If None is given instead of a
161
# tuple, all of the image is assumed.
163
def paste(self, im, box=None):
165
# convert to blittable
168
if image.isblock() and im.mode == self.__mode:
171
block = image.new_block(self.__mode, im.size)
172
image.convert2(block, image) # convert directly between buffers
177
tk.call("PyImagingPhoto", self.__photo, block.id)
178
except Tkinter.TclError, v:
179
# activate Tkinter hook
183
_imagingtk.tkinit(tk.interpaddr(), 1)
184
except AttributeError:
185
_imagingtk.tkinit(id(tk), 0)
186
tk.call("PyImagingPhoto", self.__photo, block.id)
187
except (ImportError, AttributeError, Tkinter.TclError):
188
raise # configuration problem; cannot attach to Tkinter
190
# --------------------------------------------------------------------
194
# Create a Tkinter-compatible bitmap image. This can be used
195
# everywhere Tkinter expects an image object.
200
# Create a Tkinter-compatible bitmap image.
202
# The given image must have mode "1". Pixels having value 0 are
203
# treated as transparent. Options, if any, are passed on to
204
# Tkinter. The most commonly used option is <b>foreground</b>,
205
# which is used to specify the colour for the non-transparent
206
# parts. See the Tkinter documentation for information on how to
209
# @def __init__(image=None, **options)
210
# @param image A PIL image.
212
def __init__(self, image=None, **kw):
214
# Tk compatibility: file or data
216
if kw.has_key("file"):
217
image = Image.open(kw["file"])
219
elif kw.has_key("data"):
220
from StringIO import StringIO
221
image = Image.open(StringIO(kw["data"]))
224
self.__mode = image.mode
225
self.__size = image.size
227
if _pilbitmap_check():
228
# fast way (requires the pilbitmap booster patch)
230
kw["data"] = "PIL:%d" % image.im.id
231
self.__im = image # must keep a reference
234
kw["data"] = image.tobitmap()
235
self.__photo = apply(Tkinter.BitmapImage, (), kw)
238
name = self.__photo.name
239
self.__photo.name = None
241
self.__photo.tk.call("image", "delete", name)
243
pass # ignore internal errors
246
# Get the width of the image.
248
# @return The width, in pixels.
251
return self.__size[0]
254
# Get the height of the image.
256
# @return The height, in pixels.
259
return self.__size[1]
262
# Get the Tkinter bitmap image identifier. This method is
263
# automatically called by Tkinter whenever a BitmapImage object
264
# is passed to a Tkinter method.
266
# @return A Tkinter bitmap image identifier (a string).
269
return str(self.__photo)
272
# Copies the contents of a PhotoImage to a PIL image memory.
275
photo.tk.call("PyImagingPhotoGet", photo)
277
# --------------------------------------------------------------------
278
# Helper for the Image.show method.
280
def _show(image, title):
282
class UI(Tkinter.Label):
283
def __init__(self, master, im):
285
self.image = BitmapImage(im, foreground="white", master=master)
287
self.image = PhotoImage(im, master=master)
288
Tkinter.Label.__init__(self, master, image=self.image,
291
if not Tkinter._default_root:
292
raise IOError, "tkinter not initialized"
293
top = Tkinter.Toplevel()
296
UI(top, image).pack()