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 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 | |
76 | |
77 alpha = PIL.Image.new('L', image.size, 255) | 80 alpha = PIL.Image.new('L', image.size, 255) |
78 alpha.paste(image, mask=get_alpha(image)) | 81 alpha.paste(image, mask=get_alpha(image)) |
79 alpha = PIL.ImageOps.invert(alpha) | 82 alpha = PIL.ImageOps.invert(alpha) |
80 alpha = PIL.ImageOps.autocontrast(alpha) | 83 alpha = PIL.ImageOps.autocontrast(alpha) |
81 | 84 |
82 return PIL.Image.merge('LA', [PIL.Image.new('L', image.size), alpha]) | 85 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 |
(...skipping 30 matching lines...) Expand all Loading... |
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 |