~edwin-grubbs/python-imaging/trunk

« back to all changes in this revision

Viewing changes to Scripts/pildriver.py

  • Committer: effbot
  • Date: 2006-03-01 19:11:48 UTC
  • Revision ID: svn-v4:be285980-f00d-0410-a9fe-d4747b46ecd0:pil:18
Load Imaging-1.1.3 into pil.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
space-separated tokens and passed to the execute method.
23
23
 
24
24
In the method descriptions below, a first line beginning with the string
25
 
`usage:' means this method can be invokeed with the token that follows
 
25
`usage:' means this method can be invoked with the token that follows
26
26
it.  Following <>-enclosed arguments describe how the method interprets
27
27
the entries on the stack.  Each argument specification begins with a
28
28
type specification: either `int', `float', `string', or `image'.
40
40
30 degrees, and saves the result as rotated.png (in PNG format).
41
41
"""
42
42
# by Eric S. Raymond <esr@thyrsus.com>
43
 
# $Id: pildriver,v 1.6 1998/07/19 03:45:44 esr Exp $
 
43
# $Id: //modules/pil/Scripts/pildriver.py#3 $
44
44
 
45
45
# TO DO:
46
46
# 1. Add PILFont capabilities, once that's documented.
56
56
 
57
57
    def do_verbose(self):
58
58
        """usage: verbose <int:num>
59
 
        
 
59
 
60
60
        Set verbosity flag from top of stack.
61
61
        """
62
62
        self.verbose = self.do_pop()
77
77
 
78
78
    def do_clear(self):
79
79
        """usage: clear
80
 
        
 
80
 
81
81
        Clear the stack.
82
82
        """
83
83
        self.stack = []
84
84
 
85
85
    def do_pop(self):
86
86
        """usage: pop
87
 
        
 
87
 
88
88
        Discard the top element on the stack.
89
89
        """
90
90
        top = self.stack[0]
93
93
 
94
94
    def do_dup(self):
95
95
        """usage: dup
96
 
        
 
96
 
97
97
        Duplicate the top-of-stack item.
98
98
        """
99
 
        if hasattr(self, 'format'):     # If it's an image, do a real copy 
 
99
        if hasattr(self, 'format'):     # If it's an image, do a real copy
100
100
            dup = self.stack[0].copy()
101
101
        else:
102
102
             dup = self.stack[0]
104
104
 
105
105
    def do_swap(self):
106
106
        """usage: swap
107
 
        
 
107
 
108
108
        Swap the top-of-stack item with the next one down.
109
109
        """
110
110
        self.stack = [self.stack[1], self.stack[0]] + self.stack[2:]
113
113
 
114
114
    def do_new(self):
115
115
        """usage: new <int:xsize> <int:ysize> <int:color>:
116
 
        
 
116
 
117
117
        Create and push a greyscale image of given size and color.
118
118
        """
119
119
        xsize = int(self.do_pop())
123
123
 
124
124
    def do_open(self):
125
125
        """usage: open <string:filename>
126
 
        
 
126
 
127
127
        Open the indicated image, read it, push the image on the stack.
128
128
        """
129
129
        self.push(Image.open(self.do_pop()))
130
130
 
131
131
    def do_blend(self):
132
132
        """usage: blend <image:pic1> <image:pic2> <float:alpha>
133
 
        
 
133
 
134
134
        Replace two images and an alpha with the blended image.
135
135
        """
136
136
        image1 = self.do_pop()
140
140
 
141
141
    def do_composite(self):
142
142
        """usage: composite <image:pic1> <image:pic2> <image:mask>
143
 
        
 
143
 
144
144
        Replace two images and a mask with their composite.
145
145
        """
146
146
        image1 = self.do_pop()
150
150
 
151
151
    def do_merge(self):
152
152
        """usage: merge <string:mode> <image:pic1> [<image:pic2> [<image:pic3> [<image:pic4>]]]
153
 
        
 
153
 
154
154
        Merge top-of stack images in a way described by the mode.
155
155
        """
156
156
        mode = self.do_pop()
163
163
 
164
164
    def do_convert(self):
165
165
        """usage: convert <string:mode> <image:pic1>
166
 
        
 
166
 
167
167
        Convert the top image to the given mode.
168
168
        """
169
169
        mode = self.do_pop()
172
172
 
173
173
    def do_copy(self):
174
174
        """usage: copy <image:pic1>
175
 
        
 
175
 
176
176
        Make and push a true copy of the top image.
177
177
        """
178
178
        self.dup()
179
179
 
180
180
    def do_crop(self):
181
181
        """usage: crop <int:left> <int:upper> <int:right> <int:lower> <image:pic1>
182
 
        
 
182
 
183
183
        Crop and push a rectangular region from the current image.
184
184
        """
185
185
        left = int(self.do_pop())
191
191
 
192
192
    def do_draft(self):
193
193
        """usage: draft <string:mode> <int:xsize> <int:ysize>
194
 
        
 
194
 
195
195
        Configure the loader for a given mode and size.
196
196
        """
197
197
        mode = self.do_pop()
201
201
 
202
202
    def do_filter(self):
203
203
        """usage: filter <string:filtername> <image:pic1>
204
 
        
 
204
 
205
205
        Process the top image with the given filter.
206
206
        """
207
207
        import ImageFilter
209
209
        image = self.do_pop()
210
210
        self.push(image.filter(filter))
211
211
 
 
212
    def do_getbbox(self):
 
213
        """usage: getbbox
 
214
 
 
215
        Push left, upper, right, and lower pixel coordinates of the top image.
 
216
        """
 
217
        bounding_box = self.do_pop().getbbox()
 
218
        self.push(bounding_box[3])
 
219
        self.push(bounding_box[2])
 
220
        self.push(bounding_box[1])
 
221
        self.push(bounding_box[0])
 
222
 
 
223
    def do_getextrema(self):
 
224
        """usage: extrema
 
225
 
 
226
        Push minimum and maximum pixel values of the top image.
 
227
        """
 
228
        extrema = self.do_pop().extrema()
 
229
        self.push(extrema[1])
 
230
        self.push(extrema[0])
 
231
 
212
232
    def do_offset(self):
213
233
        """usage: offset <int:xoffset> <int:yoffset> <image:pic1>
214
 
        
 
234
 
215
235
        Offset the pixels in the top image.
216
236
        """
217
237
        xoff = int(self.do_pop())
219
239
        image = self.do_pop()
220
240
        self.push(image.offset(xoff, yoff))
221
241
 
222
 
 
223
242
    def do_paste(self):
224
243
        """usage: paste <image:figure> <int:xoffset> <int:yoffset> <image:ground>
225
 
        
 
244
 
226
245
        Paste figure image into ground with upper left at given offsets.
227
246
        """
228
247
        figure = self.do_pop()
236
255
 
237
256
    def do_resize(self):
238
257
        """usage: resize <int:xsize> <int:ysize> <image:pic1>
239
 
        
 
258
 
240
259
        Resize the top image.
241
260
        """
242
261
        ysize = int(self.do_pop())
246
265
 
247
266
    def do_rotate(self):
248
267
        """usage: rotate <int:angle> <image:pic1>
249
 
        
 
268
 
250
269
        Rotate image through a given angle
251
270
        """
252
271
        angle = int(self.do_pop())
255
274
 
256
275
    def do_save(self):
257
276
        """usage: save <string:filename> <image:pic1>
258
 
        
 
277
 
259
278
        Save image with default options.
260
279
        """
261
280
        filename = self.do_pop()
264
283
 
265
284
    def do_save2(self):
266
285
        """usage: save2 <string:filename> <string:options> <image:pic1>
267
 
        
 
286
 
268
287
        Save image with specified options.
269
288
        """
270
289
        filename = self.do_pop()
274
293
 
275
294
    def do_show(self):
276
295
        """usage: show <image:pic1>
277
 
        
 
296
 
278
297
        Display and pop the top image.
279
298
        """
280
299
        self.do_pop().show()
281
300
 
282
301
    def do_thumbnail(self):
283
302
        """usage: thumbnail <int:xsize> <int:ysize> <image:pic1>
284
 
        
 
303
 
285
304
        Modify the top image in the stack to contain a thumbnail of itself.
286
305
        """
287
306
        ysize = int(self.do_pop())
290
309
 
291
310
    def do_transpose(self):
292
311
        """usage: transpose <string:operator> <image:pic1>
293
 
        
 
312
 
294
313
        Transpose the top image.
295
314
        """
296
315
        transpose = string.upper(self.do_pop())
301
320
 
302
321
    def do_format(self):
303
322
        """usage: format <image:pic1>
304
 
        
 
323
 
305
324
        Push the format of the top image onto the stack.
306
325
        """
307
326
        self.push(self.pop().format)
308
327
 
309
328
    def do_mode(self):
310
329
        """usage: mode <image:pic1>
311
 
        
 
330
 
312
331
        Push the mode of the top image onto the stack.
313
332
        """
314
333
        self.push(self.pop().mode)
315
334
 
316
335
    def do_size(self):
317
336
        """usage: size <image:pic1>
318
 
        
 
337
 
319
338
        Push the image size on the stack as (y, x).
320
339
        """
321
340
        size = self.pop().size
326
345
 
327
346
    def do_invert(self):
328
347
        """usage: invert <image:pic1>
329
 
        
 
348
 
330
349
        Invert the top image.
331
350
        """
332
351
        import ImageChops
334
353
 
335
354
    def do_lighter(self):
336
355
        """usage: lighter <image:pic1> <image:pic2>
337
 
        
 
356
 
338
357
        Pop the two top images, push an image of the lighter pixels of both.
339
358
        """
340
359
        import ImageChops
344
363
 
345
364
    def do_darker(self):
346
365
        """usage: darker <image:pic1> <image:pic2>
347
 
        
 
366
 
348
367
        Pop the two top images, push an image of the darker pixels of both.
349
368
        """
350
369
        import ImageChops
354
373
 
355
374
    def do_difference(self):
356
375
        """usage: difference <image:pic1> <image:pic2>
357
 
        
 
376
 
358
377
        Pop the two top images, push the difference image
359
378
        """
360
379
        import ImageChops
364
383
 
365
384
    def do_multiply(self):
366
385
        """usage: multiply <image:pic1> <image:pic2>
367
 
        
 
386
 
368
387
        Pop the two top images, push the multiplication image.
369
388
        """
370
389
        import ImageChops
374
393
 
375
394
    def do_screen(self):
376
395
        """usage: screen <image:pic1> <image:pic2>
377
 
        
 
396
 
378
397
        Pop the two top images, superimpose their inverted versions.
379
398
        """
380
399
        import ImageChops
384
403
 
385
404
    def do_add(self):
386
405
        """usage: add <image:pic1> <image:pic2> <int:offset> <float:scale>
387
 
        
 
406
 
388
407
        Pop the two top images, produce the scaled sum with offset.
389
408
        """
390
409
        import ImageChops
396
415
 
397
416
    def do_subtract(self):
398
417
        """usage: subtract <image:pic1> <image:pic2> <int:offset> <float:scale>
399
 
        
 
418
 
400
419
        Pop the two top images, produce the scaled difference with offset.
401
420
        """
402
421
        import ImageChops
410
429
 
411
430
    def do_color(self):
412
431
        """usage: color <image:pic1>
413
 
        
 
432
 
414
433
        Enhance color in the top image.
415
434
        """
416
435
        import ImageEnhance
421
440
 
422
441
    def do_contrast(self):
423
442
        """usage: contrast <image:pic1>
424
 
        
 
443
 
425
444
        Enhance contrast in the top image.
426
445
        """
427
446
        import ImageEnhance
432
451
 
433
452
    def do_brightness(self):
434
453
        """usage: brightness <image:pic1>
435
 
        
 
454
 
436
455
        Enhance brightness in the top image.
437
456
        """
438
457
        import ImageEnhance
443
462
 
444
463
    def do_sharpness(self):
445
464
        """usage: sharpness <image:pic1>
446
 
        
 
465
 
447
466
        Enhance sharpness in the top image.
448
467
        """
449
468
        import ImageEnhance
473
492
                func = getattr(self, funcname)
474
493
                func()
475
494
 
476
 
if __name__ == '__main__': 
 
495
if __name__ == '__main__':
477
496
    import sys
478
497
    try:
479
498
        import readline