Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 # coding: utf-8 | |
2 | |
3 # This file is part of the Adblock Plus build tools, | |
4 # Copyright (C) 2006-2013 Eyeo GmbH | |
5 # | |
6 # Adblock Plus is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU General Public License version 3 as | |
8 # published by the Free Software Foundation. | |
9 # | |
10 # Adblock Plus is distributed in the hope that it will be useful, | |
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 # GNU General Public License for more details. | |
14 # | |
15 # You should have received a copy of the GNU General Public License | |
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
17 | |
18 import os | |
19 import re | |
20 from StringIO import StringIO | |
21 | |
22 import PIL.Image | |
23 | |
24 def get_alpha(image): | |
25 if image.mode in ('RGBA', 'LA'): | |
26 return image.split()[image.getbands().index('A')] | |
27 | |
28 if image.mode == 'P': | |
29 transparency = image.info.get('transparency') | |
30 | |
31 if transparency is not None: | |
32 table = [255] * 256 | |
33 table[transparency] = 0 | |
34 | |
35 return image.point(table, 'L') | |
36 | |
37 def ensure_same_mode(im1, im2): | |
38 # if both images already have the same mode (and palette, in | |
39 # case of mode P), don't convert anything. Images with mode P, | |
40 # and a different palette, are the only case where images | |
41 # using the same mode, will be incompatible with each other. | |
42 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal ette()): | |
43 return (im1, im2) | |
44 | |
45 # if any given image has a mode that supports colors convert both | |
46 # images to RGB or RGBA, otherwise convert both images to L or LA. | |
47 # If any given image has an alpha channel (or mode P which | |
48 # can store transparent pixels too) convert both images | |
49 # to RGBA or LA, otherwise convert both images to RGB or L. | |
50 mode = max( | |
51 PIL.Image.getmodebase(im1.mode), | |
52 PIL.Image.getmodebase(im2.mode), | |
53 | |
54 key=('L', 'RGB').index | |
55 ) | |
56 | |
57 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)): | |
58 mode += 'A' | |
59 | |
60 return ( | |
61 im1 if im1.mode == mode else im1.convert(mode), | |
62 im2 if im2.mode == mode else im2.convert(mode), | |
63 ) | |
64 | |
65 def filter_grayscale(image, baseDir): | |
66 alpha = get_alpha(image) | |
67 image = image.convert('L') | |
Felix Dahlke
2013/10/15 16:24:38
I guess I'm missing something here, shouldn't imag
Sebastian Noack
2013/10/15 17:06:56
Image.convert('LA') doesn't work reliable, in all
| |
68 | |
69 if alpha: | |
70 image.putalpha(alpha) | |
71 | |
72 return image | |
73 | |
74 def filter_contrastToAlpha(image, baseDir): | |
75 import PIL.ImageOps | |
76 | |
77 alpha = PIL.Image.new('L', image.size, 255) | |
78 alpha.paste(image, mask=get_alpha(image)) | |
79 alpha = PIL.ImageOps.invert(alpha) | |
80 alpha = PIL.ImageOps.autocontrast(alpha) | |
81 | |
82 return PIL.Image.merge('LA', [PIL.Image.new('L', image.size), alpha]) | |
83 | |
84 def filter_blend(image, baseDir, *args): | |
85 if len(args) == 2: | |
86 filename, opacity = args | |
87 | |
88 overlay = PIL.Image.open(os.path.join( | |
89 baseDir, | |
90 *filename.split('/') | |
91 )) | |
92 else: | |
93 red, green, blue, opacity = args | |
94 | |
95 overlay = PIL.Image.new('RGB', image.size, ( | |
96 int(red), | |
97 int(green), | |
98 int(blue), | |
99 )) | |
100 | |
101 # if the background image has an alpha channel copy it to | |
102 # the overlay, so that transparent areas stay transparent. | |
103 alpha = get_alpha(image) | |
104 if alpha: | |
105 overlay.putalpha(alpha) | |
106 | |
107 image, overlay = ensure_same_mode(image, overlay) | |
108 return PIL.Image.blend(image, overlay, float(opacity)) | |
109 | |
110 def convertImages(params, files): | |
111 metadata = params['metadata'] | |
112 | |
113 for filename, chain in metadata.items('convert_img'): | |
114 baseDir = os.path.dirname(metadata.option_source('convert_img', filename)) | |
115 steps = re.split(r'\s*->\s*', chain) | |
116 image = PIL.Image.open(os.path.join(baseDir, *steps.pop(0).split('/'))) | |
117 | |
118 for step in steps: | |
119 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() | |
120 args = re.split(r'\s*,\s*', args) if args else () | |
121 image = globals()['filter_' + filter](image, baseDir, *args) | |
122 | |
123 f = StringIO() | |
124 f.name = filename | |
125 image.save(f) | |
126 files[filename] = f.getvalue() | |
OLD | NEW |