Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 from StringIO import StringIO | 9 from StringIO import StringIO |
10 | 10 |
11 try: | 11 try: |
12 from PIL import Image | 12 from PIL import Image |
13 from PIL import ImageOps | 13 from PIL import ImageOps |
14 except ImportError: | 14 except ImportError: |
15 import Image | 15 import Image |
16 import ImageOps | 16 import ImageOps |
17 | 17 |
18 def get_alpha(image): | 18 def get_alpha(image): |
kzar
2015/01/27 14:25:42
I think there should be a comment here to explain
Sebastian Noack
2015/01/27 14:29:25
Frankly, converting an image without alpha channel
Sebastian Noack
2015/01/27 15:10:05
It's unneeded to document APIs in the calling code
| |
19 if image.mode not in ('RGBA', 'LA'): | 19 if image.mode in ('RGBA', 'LA'): |
20 image = image.convert('LA') | 20 return image.split()[image.getbands().index('A')] |
21 | 21 |
22 return image.split()[image.getbands().index('A')] | 22 # In order to generate an alpha channel for images using a palette, we |
23 # convert the image to grayscale+alpha. Initially, we created an alpha | |
24 # channel by replacing opaque pixels with a high mark and transparent | |
25 # pixels with a low mark. However, it turned out that you can't rely on the | |
26 # value of Image.info['transparency'] since in some cases it might be an | |
27 # unparsed string instead an int indicating the value of transparent pixels. | |
28 if image.mode == 'P' and 'transparency' in image.info: | |
29 return image.convert('LA').split()[1] | |
23 | 30 |
24 def load_image(path): | 31 def load_image(path): |
25 image = Image.open(path) | 32 image = Image.open(path) |
26 # Make sure the image is loaded, some versions of PIL load images lazily. | 33 # Make sure the image is loaded, some versions of PIL load images lazily. |
27 image.load() | 34 image.load() |
28 return image | 35 return image |
29 | 36 |
30 def ensure_same_mode(im1, im2): | 37 def ensure_same_mode(im1, im2): |
31 # if both images already have the same mode (and palette, in | 38 # if both images already have the same mode (and palette, in |
32 # 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, |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 | 115 |
109 for step in steps: | 116 for step in steps: |
110 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() | 117 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() |
111 args = re.split(r'\s*,\s*', args) if args else () | 118 args = re.split(r'\s*,\s*', args) if args else () |
112 image = globals()['filter_' + filter](image, baseDir, *args) | 119 image = globals()['filter_' + filter](image, baseDir, *args) |
113 | 120 |
114 f = StringIO() | 121 f = StringIO() |
115 f.name = filename | 122 f.name = filename |
116 image.save(f) | 123 image.save(f) |
117 files[filename] = f.getvalue() | 124 files[filename] = f.getvalue() |
LEFT | RIGHT |