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

Delta Between Two Patch Sets: imageConversion.py

Issue 29334229: Issue 3532 - Stop generating animation images at build time (Closed)
Left Patch Set: Created Jan. 21, 2016, 6:07 p.m.
Right Patch Set: Removed imageCompression.py Created Jan. 22, 2016, 3:50 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « imageCompression.py ('k') | packagerChrome.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 # coding: utf-8
2
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
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 import os
8 import re
9
10 try:
11 from PIL import Image
12 from PIL import ImageOps
13 except ImportError:
14 import Image
15 import ImageOps
16
17 from imageCompression import image_to_file
18
19 def load_image(path):
20 image = Image.open(path)
21 # Make sure the image is loaded, some versions of PIL load images lazily.
22 image.load()
23 return image
24
25 def filter_contrastToAlpha(image, baseDir):
26 # In order to generate an alpha channel for images using a palette, we must
27 # convert the image to RGBA. It's important to use RGBA, not LA (grayscale+alp ha),
28 # since PIL can't reliably convert P to LA. Also initially, we created an
29 # alpha channel by replacing opaque pixels with a high mark and transparent
30 # pixels with a low mark. However, it turned out that you can't rely on the
31 # value of Image.info['transparency'] since in case the transparency is
32 # specified in bytes (pngout loves to do that), we can't associate that value
33 # with transparent pixels. Moreover, some versions of PIL raise a warning
34 # when such images are pasted into another image.
35 if image.mode == 'P' and 'transparency' in image.info:
36 image = image.convert('RGBA')
37
38 # Image.paste() ignores the alpha channel of the pasted image, but expects
39 # the mask to be passed as separate argument. So we have to extract the alpha
40 # channel (if any) from the image we are going to paste.
41 if image.mode in ('RGBA', 'LA'):
42 mask = image.split()[image.getbands().index('A')]
43 else:
44 mask = None
45
46 alpha = Image.new('L', image.size, 255)
47 alpha.paste(image, mask=mask)
48 alpha = ImageOps.invert(alpha)
49 alpha = ImageOps.autocontrast(alpha)
50
51 return Image.merge('LA', [Image.new('L', image.size), alpha])
52
53 def convertImages(params, files):
54 metadata = params['metadata']
55
56 for filename, chain in metadata.items('convert_img'):
57 baseDir = os.path.dirname(metadata.option_source('convert_img', filename))
58 steps = re.split(r'\s*->\s*', chain)
59 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/')))
60
61 for step in steps:
62 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
63 args = re.split(r'\s*,\s*', args) if args else ()
64 image = globals()['filter_' + filter](image, baseDir, *args)
65
66 file = image_to_file(image, filename)
67 try:
68 files[filename] = file.read()
69 finally:
70 file.close()
LEFTRIGHT

Powered by Google App Engine
This is Rietveld