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