~ubuntu-branches/ubuntu/trusty/python-imaging/trusty

« back to all changes in this revision

Viewing changes to .pc/git-updates.diff/PIL/ImageTk.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-03-20 16:44:01 UTC
  • mfrom: (2.1.13 experimental)
  • Revision ID: package-import@ubuntu.com-20130320164401-ptf6m0ttg4zw72az
Tags: 1.1.7+2.0.0-1
Pillow 2.0.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#
2
 
# The Python Imaging Library.
3
 
# $Id$
4
 
#
5
 
# a Tk display interface
6
 
#
7
 
# History:
8
 
# 96-04-08 fl   Created
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
21
 
#
22
 
# Copyright (c) 1997-1999 by Secret Labs AB
23
 
# Copyright (c) 1996-1997 by Fredrik Lundh
24
 
#
25
 
# See the README file for information on usage and redistribution.
26
 
#
27
 
 
28
 
import Tkinter, Image
29
 
 
30
 
##
31
 
# The <b>ImageTk</b> module contains support to create and modify
32
 
# Tkinter <b>BitmapImage</b> and <b>PhotoImage</b> objects.
33
 
# <p>
34
 
# For examples, see the demo programs in the <i>Scripts</i>
35
 
# directory.
36
 
##
37
 
 
38
 
# --------------------------------------------------------------------
39
 
# Check for Tkinter interface hooks
40
 
 
41
 
_pilbitmap_ok = None
42
 
 
43
 
def _pilbitmap_check():
44
 
    global _pilbitmap_ok
45
 
    if _pilbitmap_ok is None:
46
 
        try:
47
 
            im = Image.new("1", (1,1))
48
 
            Tkinter.BitmapImage(data="PIL:%d" % im.im.id)
49
 
            _pilbitmap_ok = 1
50
 
        except Tkinter.TclError:
51
 
            _pilbitmap_ok = 0
52
 
    return _pilbitmap_ok
53
 
 
54
 
# --------------------------------------------------------------------
55
 
# PhotoImage
56
 
 
57
 
##
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.
61
 
 
62
 
class PhotoImage:
63
 
 
64
 
    ##
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.
69
 
    # <p>
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
76
 
    #    Image.open(file)).
77
 
    # @keyparam data An 8-bit string containing image data (as
78
 
    #    loaded from an image file).
79
 
 
80
 
    def __init__(self, image=None, size=None, **kw):
81
 
 
82
 
        # Tk compatibility: file or data
83
 
        if image is None:
84
 
            if kw.has_key("file"):
85
 
                image = Image.open(kw["file"])
86
 
                del kw["file"]
87
 
            elif kw.has_key("data"):
88
 
                from StringIO import StringIO
89
 
                image = Image.open(StringIO(kw["data"]))
90
 
                del kw["data"]
91
 
 
92
 
        if hasattr(image, "mode") and hasattr(image, "size"):
93
 
            # got an image instead of a mode
94
 
            mode = image.mode
95
 
            if mode == "P":
96
 
                # palette mapped data
97
 
                image.load()
98
 
                try:
99
 
                    mode = image.palette.mode
100
 
                except AttributeError:
101
 
                    mode = "RGB" # default
102
 
            size = image.size
103
 
            kw["width"], kw["height"] = size
104
 
        else:
105
 
            mode = image
106
 
            image = None
107
 
 
108
 
        if mode not in ["1", "L", "RGB", "RGBA"]:
109
 
            mode = Image.getmodebase(mode)
110
 
 
111
 
        self.__mode = mode
112
 
        self.__size = size
113
 
        self.__photo = apply(Tkinter.PhotoImage, (), kw)
114
 
        self.tk = self.__photo.tk
115
 
        if image:
116
 
            self.paste(image)
117
 
 
118
 
    def __del__(self):
119
 
        name = self.__photo.name
120
 
        self.__photo.name = None
121
 
        try:
122
 
            self.__photo.tk.call("image", "delete", name)
123
 
        except:
124
 
            pass # ignore internal errors
125
 
 
126
 
    ##
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.
130
 
    #
131
 
    # @return A Tkinter photo image identifier (a string).
132
 
 
133
 
    def __str__(self):
134
 
        return str(self.__photo)
135
 
 
136
 
    ##
137
 
    # Get the width of the image.
138
 
    #
139
 
    # @return The width, in pixels.
140
 
 
141
 
    def width(self):
142
 
        return self.__size[0]
143
 
 
144
 
    ##
145
 
    # Get the height of the image.
146
 
    #
147
 
    # @return The height, in pixels.
148
 
 
149
 
    def height(self):
150
 
        return self.__size[1]
151
 
 
152
 
    ##
153
 
    # Paste a PIL image into the photo image.  Note that this can
154
 
    # be very slow if the photo image is displayed.
155
 
    #
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.
162
 
 
163
 
    def paste(self, im, box=None):
164
 
 
165
 
        # convert to blittable
166
 
        im.load()
167
 
        image = im.im
168
 
        if image.isblock() and im.mode == self.__mode:
169
 
            block = image
170
 
        else:
171
 
            block = image.new_block(self.__mode, im.size)
172
 
            image.convert2(block, image) # convert directly between buffers
173
 
 
174
 
        tk = self.__photo.tk
175
 
 
176
 
        try:
177
 
            tk.call("PyImagingPhoto", self.__photo, block.id)
178
 
        except Tkinter.TclError, v:
179
 
            # activate Tkinter hook
180
 
            try:
181
 
                import _imagingtk
182
 
                try:
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
189
 
 
190
 
# --------------------------------------------------------------------
191
 
# BitmapImage
192
 
 
193
 
##
194
 
# Create a Tkinter-compatible bitmap image.  This can be used
195
 
# everywhere Tkinter expects an image object.
196
 
 
197
 
class BitmapImage:
198
 
 
199
 
    ##
200
 
    # Create a Tkinter-compatible bitmap image.
201
 
    # <p>
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
207
 
    # specify colours.
208
 
    #
209
 
    # @def __init__(image=None, **options)
210
 
    # @param image A PIL image.
211
 
 
212
 
    def __init__(self, image=None, **kw):
213
 
 
214
 
        # Tk compatibility: file or data
215
 
        if image is None:
216
 
            if kw.has_key("file"):
217
 
                image = Image.open(kw["file"])
218
 
                del kw["file"]
219
 
            elif kw.has_key("data"):
220
 
                from StringIO import StringIO
221
 
                image = Image.open(StringIO(kw["data"]))
222
 
                del kw["data"]
223
 
 
224
 
        self.__mode = image.mode
225
 
        self.__size = image.size
226
 
 
227
 
        if _pilbitmap_check():
228
 
            # fast way (requires the pilbitmap booster patch)
229
 
            image.load()
230
 
            kw["data"] = "PIL:%d" % image.im.id
231
 
            self.__im = image # must keep a reference
232
 
        else:
233
 
            # slow but safe way
234
 
            kw["data"] = image.tobitmap()
235
 
        self.__photo = apply(Tkinter.BitmapImage, (), kw)
236
 
 
237
 
    def __del__(self):
238
 
        name = self.__photo.name
239
 
        self.__photo.name = None
240
 
        try:
241
 
            self.__photo.tk.call("image", "delete", name)
242
 
        except:
243
 
            pass # ignore internal errors
244
 
 
245
 
    ##
246
 
    # Get the width of the image.
247
 
    #
248
 
    # @return The width, in pixels.
249
 
 
250
 
    def width(self):
251
 
        return self.__size[0]
252
 
 
253
 
    ##
254
 
    # Get the height of the image.
255
 
    #
256
 
    # @return The height, in pixels.
257
 
 
258
 
    def height(self):
259
 
        return self.__size[1]
260
 
 
261
 
    ##
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.
265
 
    #
266
 
    # @return A Tkinter bitmap image identifier (a string).
267
 
 
268
 
    def __str__(self):
269
 
        return str(self.__photo)
270
 
 
271
 
##
272
 
# Copies the contents of a PhotoImage to a PIL image memory.
273
 
 
274
 
def getimage(photo):
275
 
    photo.tk.call("PyImagingPhotoGet", photo)
276
 
 
277
 
# --------------------------------------------------------------------
278
 
# Helper for the Image.show method.
279
 
 
280
 
def _show(image, title):
281
 
 
282
 
    class UI(Tkinter.Label):
283
 
        def __init__(self, master, im):
284
 
            if im.mode == "1":
285
 
                self.image = BitmapImage(im, foreground="white", master=master)
286
 
            else:
287
 
                self.image = PhotoImage(im, master=master)
288
 
            Tkinter.Label.__init__(self, master, image=self.image,
289
 
                bg="black", bd=0)
290
 
 
291
 
    if not Tkinter._default_root:
292
 
        raise IOError, "tkinter not initialized"
293
 
    top = Tkinter.Toplevel()
294
 
    if title:
295
 
        top.title(title)
296
 
    UI(top, image).pack()