Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: imageConversion.py

Issue 5195120659922944: Issue 1883 - Convert mode P to LA to work around a PIL bug when extracting the alpha channel (Closed)
Patch Set: Single quotes for consistency Created Jan. 27, 2015, 2:19 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 in ('RGBA', 'LA'): 19 if image.mode not in ('RGBA', 'LA'):
20 return image.split()[image.getbands().index('A')] 20 image = image.convert('LA')
21 21
22 if image.mode == 'P': 22 return image.split()[image.getbands().index('A')]
23 transparency = image.info.get('transparency')
24
25 if transparency is not None:
26 table = [255] * 256
27 table[transparency] = 0
28
29 return image.point(table, 'L')
30 23
31 def load_image(path): 24 def load_image(path):
32 image = Image.open(path) 25 image = Image.open(path)
33 # Make sure the image is loaded, some versions of PIL load images lazily. 26 # Make sure the image is loaded, some versions of PIL load images lazily.
34 image.load() 27 image.load()
35 return image 28 return image
36 29
37 def ensure_same_mode(im1, im2): 30 def ensure_same_mode(im1, im2):
38 # if both images already have the same mode (and palette, in 31 # if both images already have the same mode (and palette, in
39 # case of mode P), don't convert anything. Images with mode P, 32 # case of mode P), don't convert anything. Images with mode P,
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 108
116 for step in steps: 109 for step in steps:
117 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() 110 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
118 args = re.split(r'\s*,\s*', args) if args else () 111 args = re.split(r'\s*,\s*', args) if args else ()
119 image = globals()['filter_' + filter](image, baseDir, *args) 112 image = globals()['filter_' + filter](image, baseDir, *args)
120 113
121 f = StringIO() 114 f = StringIO()
122 f.name = filename 115 f.name = filename
123 image.save(f) 116 image.save(f)
124 files[filename] = f.getvalue() 117 files[filename] = f.getvalue()
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld