fix
[redakcja.git] / scripts / crop.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
5 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6 #
7 import sys
8 import os
9 from os.path import splitext, dirname, basename, realpath
10 from PIL import Image
11
12
13 def crop(image, top=0, right=0, bottom=0, left=0):
14     width, height = image.size
15     if top < 1:
16         top = int(height * top)
17     if right < 1:
18         right = int(width * right)
19     if bottom < 1:
20         bottom = int(height * bottom)
21     if left < 1:
22         left = int(width * left)
23
24     bounds = (int(left), int(top), int(width - right), int(height - bottom))
25     image = image.crop(bounds)
26     image.load()
27     return image
28
29 output_dir = realpath(os.getcwd()) + '/output'
30 bounds = [float(i) for i in sys.argv[1].split(':')]
31
32 for file_name in sys.argv[2:]:
33     base_name, ext = splitext(file_name)
34     try:
35         image = Image.open(file_name)
36     except IOError, e:
37         sys.stderr.write('\nerror:%s:%s\n' % (file_name, e.message))
38         continue
39
40     image = crop(image, *bounds)
41     image.save(output_dir + '/' + basename(file_name))