Przeniesienie skryptów do nowego katalogu scripts.
[redakcja.git] / scripts / crop.py
1 #!/usr/bin/env python
2 import sys
3 import os
4 from os.path import splitext, dirname, basename, realpath
5 from PIL import Image
6
7
8 def crop(image, top=0, right=0, bottom=0, left=0):
9     width, height = image.size
10     if top < 1:
11         top = int(height * top)
12     if right < 1:
13         right = int(width * right)
14     if bottom < 1:
15         bottom = int(height * bottom)
16     if left < 1:
17         left = int(width * left)
18     
19     bounds = (int(left), int(top), int(width - right), int(height - bottom))
20     image = image.crop(bounds)
21     image.load()
22     return image
23
24
25 output_dir = realpath(os.getcwd()) + '/output'
26 bounds = [float(i) for i in sys.argv[1].split(':')]
27
28 for file_name in sys.argv[2:]:
29     base_name, ext = splitext(file_name)
30     try:
31         image = Image.open(file_name)
32     except IOError, e:
33         sys.stderr.write('\nerror:%s:%s\n' % (file_name, e.message))
34         continue
35     
36     image = crop(image, *bounds)
37     image.save(output_dir + '/' + basename(file_name))