| LEFT | RIGHT |
| 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 import PIL.Image |
| 23 import PIL.ImageOps | |
| 24 | 23 |
| 25 def get_alpha(image): | 24 def get_alpha(image): |
| 26 if image.mode in ('RGBA', 'LA'): | 25 if image.mode in ('RGBA', 'LA'): |
| 27 return image.split()[image.getbands().index('A')] | 26 return image.split()[image.getbands().index('A')] |
| 28 | 27 |
| 29 # images with palette doesn't have an alpha channel, but | |
| 30 # can store transparent pixels by using 0 as image data. | |
| 31 # In order to don't discard those information, we create an | |
| 32 # alpha channel from all the pixels that are not transparent. | |
| 33 if image.mode == 'P': | 28 if image.mode == 'P': |
| 34 return image.point([0] + [255] * 255, 'L') | 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') |
| 35 | 36 |
| 36 def ensure_same_mode(im1, im2): | 37 def ensure_same_mode(im1, im2): |
| 37 # if both images already have the same mode (and palette, in | 38 # if both images already have the same mode (and palette, in |
| 38 # case of mode P), don't convert anything. Images with mode P, | 39 # case of mode P), don't convert anything. Images with mode P, |
| 39 # and a different palette, are the only case where images | 40 # and a different palette, are the only case where images |
| 40 # using the same mode, will be incompatible with each other. | 41 # using the same mode, will be incompatible with each other. |
| 41 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal
ette()): | 42 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal
ette()): |
| 42 return (im1, im2) | 43 return (im1, im2) |
| 43 | 44 |
| 44 # if any given image has a mode that supports colors convert both | 45 # if any given image has a mode that supports colors convert both |
| (...skipping 19 matching lines...) Expand all Loading... |
| 64 def filter_grayscale(image, baseDir): | 65 def filter_grayscale(image, baseDir): |
| 65 alpha = get_alpha(image) | 66 alpha = get_alpha(image) |
| 66 image = image.convert('L') | 67 image = image.convert('L') |
| 67 | 68 |
| 68 if alpha: | 69 if alpha: |
| 69 image.putalpha(alpha) | 70 image.putalpha(alpha) |
| 70 | 71 |
| 71 return image | 72 return image |
| 72 | 73 |
| 73 def filter_contrastToAlpha(image, baseDir): | 74 def filter_contrastToAlpha(image, baseDir): |
| 75 import PIL.ImageOps |
| 76 |
| 74 alpha = PIL.Image.new('L', image.size, 255) | 77 alpha = PIL.Image.new('L', image.size, 255) |
| 75 alpha.paste(image, mask=get_alpha(image)) | 78 alpha.paste(image, mask=get_alpha(image)) |
| 76 alpha = PIL.ImageOps.invert(alpha) | 79 alpha = PIL.ImageOps.invert(alpha) |
| 77 alpha = PIL.ImageOps.autocontrast(alpha) | 80 alpha = PIL.ImageOps.autocontrast(alpha) |
| 78 | 81 |
| 79 return PIL.Image.merge('LA', [PIL.Image.new('L', image.size), alpha]) | 82 return PIL.Image.merge('LA', [PIL.Image.new('L', image.size), alpha]) |
| 80 | 83 |
| 81 def filter_blend(image, baseDir, *args): | 84 def filter_blend(image, baseDir, *args): |
| 82 if len(args) == 2: | 85 if len(args) == 2: |
| 83 filename, opacity = args | 86 filename, opacity = args |
| (...skipping 30 matching lines...) Expand all Loading... |
| 114 | 117 |
| 115 for step in steps: | 118 for step in steps: |
| 116 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() | 119 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() |
| 117 args = re.split(r'\s*,\s*', args) if args else () | 120 args = re.split(r'\s*,\s*', args) if args else () |
| 118 image = globals()['filter_' + filter](image, baseDir, *args) | 121 image = globals()['filter_' + filter](image, baseDir, *args) |
| 119 | 122 |
| 120 f = StringIO() | 123 f = StringIO() |
| 121 f.name = filename | 124 f.name = filename |
| 122 image.save(f) | 125 image.save(f) |
| 123 files[filename] = f.getvalue() | 126 files[filename] = f.getvalue() |
| LEFT | RIGHT |