ce514b32c70f72c2e769c4383e49848019f7eb68
[redakcja.git] / scripts / imgconv.py
1 #!/usr/bin/env python
2 import sys
3 import os
4 import shutil
5 from os.path import splitext, dirname, basename, realpath
6 from PIL import Image, ImageFilter, ImageEnhance, ImageOps
7
8
9 def resize(image, max_width, max_height):
10     """Resize image so it's not wider than max_width and not higher than max_height."""
11     width, height = image.size
12     ratio = max(1.0, float(width) / max_width, float(height) / max_height)
13     new_width, new_height = int(width / ratio), int(height / ratio)
14     return image.resize((new_width, new_height), Image.ANTIALIAS)
15
16
17 def crop(image, ratio, from_right=False):
18     """Crop image to ratio of current width."""
19     width, height = image.size
20     new_width = width * ratio
21     if from_right:
22         bounds = (int(width - new_width), 0, int(width), int(height))
23     else:
24         bounds = (0, 0, int(new_width), int(height))
25     image = image.crop(bounds)
26     image.load()
27     return image
28
29
30 def ratio(image):
31     """Return width to height ratio of image."""
32     width, height = image.size
33     return float(width) / height
34     
35
36 def try_creating(directory):
37     try:
38         os.mkdir(directory)
39     except:
40         pass
41
42
43 output_dir = realpath(os.getcwd()) + '/output'
44 # big_output_dir = output_dir + '/big'
45 tmp_output_dir = output_dir + '/tmp'
46
47 try_creating(output_dir)
48 # try_creating(big_output_dir)
49 try_creating(tmp_output_dir)
50
51
52 for file_name in sys.argv[1:]:
53     base_name, ext = splitext(file_name)
54     try:
55         image = Image.open(file_name)
56     except IOError, e:
57         sys.stderr.write('\nerror:%s:%s\n' % (file_name, e.message))
58         continue
59     
60     # Check ratio
61     if ratio(image) > 1:
62         images = [crop(image, 0.6), crop(image, 0.6, from_right=True)]
63     else:
64         images = [image]
65     
66     for i, image in enumerate(images):
67         image_name = '%s.%d.png' % (basename(base_name), i)
68         
69         # Save files
70         small_image = resize(image, 640, 960)
71         small_image = small_image.convert('L')
72         small_image = ImageOps.autocontrast(small_image, cutoff=85)
73         # small_image = small_image.filter(ImageFilter.SHARPEN)
74         small_image.save(tmp_output_dir + '/' + image_name)
75
76         os.system('pngnq -n 128 -s 1 -e .png -d "%s" -f "%s"' % (
77             output_dir,
78             tmp_output_dir + '/' + image_name,
79         ))
80         os.remove(tmp_output_dir + '/' + image_name)
81         
82         # big_image = resize(image, 960, 1440)
83         # big_image = big_image.convert('L')
84         # big_image = big_image.filter(ImageFilter.SHARPEN)
85         # big_image.save(tmp_output_dir + '/' + image_name, optimize=True)
86         # os.system('pngnq -n 16 -s 1 -e .png -d "%s" -f "%s"' % (
87         #     big_output_dir,
88         #     tmp_output_dir + '/' + image_name,
89         # ))
90         # os.remove(tmp_output_dir + '/' + image_name)
91         
92     sys.stderr.write('.')
93
94 shutil.rmtree(tmp_output_dir)