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

Side by Side Diff: imageConversion.py

Issue 4663693082099712: Issue 1897 - Compress auto-generated images with pngout (Closed)
Patch Set: Created Jan. 28, 2015, 9:40 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
« imageCompression.py ('K') | « imageCompression.py ('k') | 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
10 9
11 try: 10 try:
12 from PIL import Image 11 from PIL import Image
13 from PIL import ImageOps 12 from PIL import ImageOps
14 except ImportError: 13 except ImportError:
15 import Image 14 import Image
16 import ImageOps 15 import ImageOps
17 16
17 from imageCompression import image_to_file
18
18 def get_alpha(image): 19 def get_alpha(image):
19 if image.mode in ('RGBA', 'LA'): 20 if image.mode in ('RGBA', 'LA'):
20 return image.split()[image.getbands().index('A')] 21 return image.split()[image.getbands().index('A')]
21 22
22 # In order to generate an alpha channel for images using a palette, we 23 # In order to generate an alpha channel for images using a palette, we
23 # convert the image to RGBA. It's important to use RGBA, not LA (grayscale+alp ha), 24 # convert the image to RGBA. It's important to use RGBA, not LA (grayscale+alp ha),
24 # since PIL can't reliably convert P to LA. Also initially, we created an 25 # since PIL can't reliably convert P to LA. Also initially, we created an
25 # alpha channel by replacing opaque pixels with a high mark and transparent 26 # alpha channel by replacing opaque pixels with a high mark and transparent
26 # pixels with a low mark. However, it turned out that you can't rely on the 27 # pixels with a low mark. However, it turned out that you can't rely on the
27 # value of Image.info['transparency'] since in some cases it might be an 28 # value of Image.info['transparency'] since in some cases it might be an
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 for filename, chain in metadata.items('convert_img'): 89 for filename, chain in metadata.items('convert_img'):
89 baseDir = os.path.dirname(metadata.option_source('convert_img', filename)) 90 baseDir = os.path.dirname(metadata.option_source('convert_img', filename))
90 steps = re.split(r'\s*->\s*', chain) 91 steps = re.split(r'\s*->\s*', chain)
91 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/'))) 92 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/')))
92 93
93 for step in steps: 94 for step in steps:
94 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() 95 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
95 args = re.split(r'\s*,\s*', args) if args else () 96 args = re.split(r'\s*,\s*', args) if args else ()
96 image = globals()['filter_' + filter](image, baseDir, *args) 97 image = globals()['filter_' + filter](image, baseDir, *args)
97 98
98 f = StringIO() 99 file = image_to_file(image, filename)
99 f.name = filename 100 try:
100 image.save(f) 101 files[filename] = file.read()
101 files[filename] = f.getvalue() 102 finally:
103 file.close()
OLDNEW
« imageCompression.py ('K') | « imageCompression.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld