diff options
Diffstat (limited to 'thumbnailer.py')
-rwxr-xr-x | thumbnailer.py | 125 |
1 files changed, 125 insertions, 0 deletions
diff --git a/thumbnailer.py b/thumbnailer.py new file mode 100755 index 0000000..cebbf72 --- /dev/null +++ b/thumbnailer.py | |||
@@ -0,0 +1,125 @@ | |||
1 | #!/usr/bin/env python2.6 | ||
2 | |||
3 | import hmac | ||
4 | import urllib2 | ||
5 | import urlparse | ||
6 | from cStringIO import StringIO | ||
7 | |||
8 | from PIL import Image, ImageOps | ||
9 | from PIL.ImageFileIO import ImageFileIO | ||
10 | |||
11 | |||
12 | def crop_image(img, width, height): | ||
13 | src_width, src_height = img.size | ||
14 | src_ratio = float(src_width) / float(src_height) | ||
15 | dst_width, dst_height = int(width), int(height) | ||
16 | dst_ratio = float(dst_width) / float(dst_height) | ||
17 | |||
18 | if dst_ratio < src_ratio: | ||
19 | crop_height = src_height | ||
20 | crop_width = crop_height * dst_ratio | ||
21 | x_offset = float(src_width - crop_width) / 2 | ||
22 | y_offset = 0 | ||
23 | else: | ||
24 | crop_width = src_width | ||
25 | crop_height = crop_width / dst_ratio | ||
26 | x_offset = 0 | ||
27 | y_offset = float(src_height - crop_height) / 3 | ||
28 | |||
29 | img = img.crop((x_offset, y_offset, x_offset+int(crop_width), y_offset+int(crop_height))) | ||
30 | |||
31 | return img | ||
32 | |||
33 | |||
34 | def calculate_size(img, long_side): | ||
35 | width, height = img.size | ||
36 | width, height = float(width), float(height) | ||
37 | |||
38 | if height > width: | ||
39 | width = (width / height) * long_side | ||
40 | height = long_side | ||
41 | else: | ||
42 | height = (height / width) * long_side | ||
43 | width = long_side | ||
44 | |||
45 | return width, height | ||
46 | |||
47 | |||
48 | def load_remote_image(url): | ||
49 | return Image.open(ImageFileIO(urllib2.urlopen(url))) | ||
50 | |||
51 | |||
52 | def create_thumbnail(img, long_side): | ||
53 | width, height = calculate_size(img, int(long_side)) | ||
54 | img.thumbnail((width, height), Image.ANTIALIAS) | ||
55 | return img | ||
56 | |||
57 | |||
58 | def desaturate_image(img): | ||
59 | img = ImageOps.grayscale(img) | ||
60 | return img | ||
61 | |||
62 | |||
63 | KEY = "" | ||
64 | |||
65 | OPERATIONS = { | ||
66 | "thumb": (create_thumbnail, 1), | ||
67 | "crop": (crop_image, 2), | ||
68 | "desaturate": (desaturate_image, 0), | ||
69 | } | ||
70 | |||
71 | def generate_sig(query): | ||
72 | clean = ["{0}{1}".format(k,v) for k, v in query if k != "sig"] | ||
73 | return hmac.new(KEY, "".join(clean)).hexdigest() | ||
74 | |||
75 | |||
76 | def thumbnailer_app(environ, start_response): | ||
77 | to_do = [] | ||
78 | qs = [] | ||
79 | for tup in urlparse.parse_qsl(environ.get('QUERY_STRING', '')): | ||
80 | if tup[0] == "sig": | ||
81 | sig = tup[1] | ||
82 | else: | ||
83 | qs.append(tup) | ||
84 | |||
85 | # Validate | ||
86 | if generate_sig(qs) != sig: | ||
87 | start_response('401 Forbidden', [('Content-Type', 'text/plain')]) | ||
88 | return ["You may not use the thumbnailer for that site."] | ||
89 | |||
90 | need_values = 0 | ||
91 | for key, value in qs: | ||
92 | if need_values > 0: | ||
93 | to_do[-1].append(value) | ||
94 | need_values -= 1 | ||
95 | continue | ||
96 | |||
97 | if key == "image": | ||
98 | path = value | ||
99 | elif key == "op": | ||
100 | op, args = OPERATIONS[value] | ||
101 | need_values = args | ||
102 | to_do.append([op]) | ||
103 | |||
104 | # Start the web response | ||
105 | start_response('200 OK', [('Content-Type', 'image/jpeg')]) | ||
106 | |||
107 | img = load_remote_image(path) | ||
108 | |||
109 | for action in to_do: | ||
110 | func = action.pop(0) | ||
111 | img = func(img, *action) | ||
112 | |||
113 | return img.tostring('jpeg', img.mode) | ||
114 | |||
115 | |||
116 | if __name__ == "__main__": | ||
117 | import sys | ||
118 | |||
119 | if len(sys.argv) == 2: | ||
120 | qs = urlparse.parse_qsl(urlparse.urlparse(sys.argv[-1]).query) | ||
121 | print "{0}&sig={1}".format(sys.argv[-1], generate_sig(qs)) | ||
122 | sys.exit(0) | ||
123 | |||
124 | from flup.server.fcgi_fork import WSGIServer | ||
125 | WSGIServer(thumbnailer_app, debug=True, maxSpare=1).run() | ||