OLD | NEW |
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This Source Code Form is subject to the terms of the Mozilla Public | 3 # This Source Code Form is subject to the terms of the Mozilla Public |
4 # License, v. 2.0. If a copy of the MPL was not distributed with this | 4 # License, v. 2.0. If a copy of the MPL was not distributed with this |
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. | 5 # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
6 | 6 |
7 import os | 7 import os |
8 import re | 8 import re |
9 | 9 |
10 try: | 10 try: |
11 from PIL import Image | 11 from PIL import Image |
12 from PIL import ImageOps | 12 from PIL import ImageOps |
13 except ImportError: | 13 except ImportError: |
14 import Image | 14 import Image |
15 import ImageOps | 15 import ImageOps |
16 | 16 |
17 from imageCompression import image_to_file | 17 from imageCompression import image_to_file |
18 | 18 |
19 def get_alpha(image): | |
20 if image.mode in ('RGBA', 'LA'): | |
21 return image.split()[image.getbands().index('A')] | |
22 | |
23 # In order to generate an alpha channel for images using a palette, we | |
24 # convert the image to RGBA. It's important to use RGBA, not LA (grayscale+alp
ha), | |
25 # since PIL can't reliably convert P to LA. Also initially, we created an | |
26 # alpha channel by replacing opaque pixels with a high mark and transparent | |
27 # pixels with a low mark. However, it turned out that you can't rely on the | |
28 # value of Image.info['transparency'] since in some cases it might be an | |
29 # unparsed string instead an int indicating the value of transparent pixels. | |
30 if image.mode == 'P' and 'transparency' in image.info: | |
31 return image.convert('RGBA').split()[3] | |
32 | |
33 def load_image(path): | 19 def load_image(path): |
34 image = Image.open(path) | 20 image = Image.open(path) |
35 # Make sure the image is loaded, some versions of PIL load images lazily. | 21 # Make sure the image is loaded, some versions of PIL load images lazily. |
36 image.load() | 22 image.load() |
37 return image | 23 return image |
38 | 24 |
39 def ensure_same_mode(im1, im2): | 25 def ensure_same_mode(im1, im2): |
40 # if both images already have the same mode (and palette, in | 26 # if both images already have the same mode (and palette, in |
41 # case of mode P), don't convert anything. Images with mode P, | 27 # case of mode P), don't convert anything. Images with mode P, |
42 # and a different palette, are the only case where images | 28 # and a different palette, are the only case where images |
(...skipping 15 matching lines...) Expand all Loading... |
58 | 44 |
59 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)): | 45 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)): |
60 mode += 'A' | 46 mode += 'A' |
61 | 47 |
62 return ( | 48 return ( |
63 im1 if im1.mode == mode else im1.convert(mode), | 49 im1 if im1.mode == mode else im1.convert(mode), |
64 im2 if im2.mode == mode else im2.convert(mode), | 50 im2 if im2.mode == mode else im2.convert(mode), |
65 ) | 51 ) |
66 | 52 |
67 def filter_contrastToAlpha(image, baseDir): | 53 def filter_contrastToAlpha(image, baseDir): |
| 54 # In order to generate an alpha channel for images using a palette, we must |
| 55 # convert the image to RGBA. It's important to use RGBA, not LA (grayscale+alp
ha), |
| 56 # since PIL can't reliably convert P to LA. Also initially, we created an |
| 57 # alpha channel by replacing opaque pixels with a high mark and transparent |
| 58 # pixels with a low mark. However, it turned out that you can't rely on the |
| 59 # value of Image.info['transparency'] since in case the transparency is |
| 60 # specified in bytes (pngout loves to do that), we can't associate that value |
| 61 # with transparent pixels. Moreover, some versions of PIL raise a warning |
| 62 # when such images are pasted into another image. |
| 63 if image.mode == 'P' and 'transparency' in image.info: |
| 64 image = image.convert('RGBA') |
| 65 |
| 66 # Image.paste() ignores the alpha channel of the pasted image, but expects |
| 67 # the mask to be passed as separate argument. So we have to extract the alpha |
| 68 # channel (if any) from the image we are going to paste. |
| 69 if image.mode in ('RGBA', 'LA'): |
| 70 mask = image.split()[image.getbands().index('A')] |
| 71 else: |
| 72 mask = None |
| 73 |
68 alpha = Image.new('L', image.size, 255) | 74 alpha = Image.new('L', image.size, 255) |
69 alpha.paste(image, mask=get_alpha(image)) | 75 alpha.paste(image, mask=mask) |
70 alpha = ImageOps.invert(alpha) | 76 alpha = ImageOps.invert(alpha) |
71 alpha = ImageOps.autocontrast(alpha) | 77 alpha = ImageOps.autocontrast(alpha) |
72 | 78 |
73 return Image.merge('LA', [Image.new('L', image.size), alpha]) | 79 return Image.merge('LA', [Image.new('L', image.size), alpha]) |
74 | 80 |
75 def filter_blend(image, baseDir, filename, opacity): | 81 def filter_blend(image, baseDir, filename, opacity): |
76 image, overlay = ensure_same_mode( | 82 image, overlay = ensure_same_mode( |
77 image, | 83 image, |
78 load_image(os.path.join( | 84 load_image(os.path.join( |
79 baseDir, | 85 baseDir, |
(...skipping 14 matching lines...) Expand all Loading... |
94 for step in steps: | 100 for step in steps: |
95 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() | 101 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() |
96 args = re.split(r'\s*,\s*', args) if args else () | 102 args = re.split(r'\s*,\s*', args) if args else () |
97 image = globals()['filter_' + filter](image, baseDir, *args) | 103 image = globals()['filter_' + filter](image, baseDir, *args) |
98 | 104 |
99 file = image_to_file(image, filename) | 105 file = image_to_file(image, filename) |
100 try: | 106 try: |
101 files[filename] = file.read() | 107 files[filename] = file.read() |
102 finally: | 108 finally: |
103 file.close() | 109 file.close() |
OLD | NEW |