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