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

Side by Side Diff: imageConversion.py

Issue 29334229: Issue 3532 - Stop generating animation images at build time (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « imageCompression.py ('k') | packagerChrome.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 ensure_same_mode(im1, im2):
26 # if both images already have the same mode (and palette, in
27 # case of mode P), don't convert anything. Images with mode P,
28 # and a different palette, are the only case where images
29 # using the same mode, will be incompatible with each other.
30 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal ette()):
31 return (im1, im2)
32
33 # if any given image has a mode that supports colors convert both
34 # images to RGB or RGBA, otherwise convert both images to L or LA.
35 # If any given image has an alpha channel (or mode P which
36 # can store transparent pixels too) convert both images
37 # to RGBA or LA, otherwise convert both images to RGB or L.
38 mode = max(
39 Image.getmodebase(im1.mode),
40 Image.getmodebase(im2.mode),
41
42 key=('L', 'RGB').index
43 )
44
45 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)):
46 mode += 'A'
47
48 return (
49 im1 if im1.mode == mode else im1.convert(mode),
50 im2 if im2.mode == mode else im2.convert(mode),
51 )
52
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
74 alpha = Image.new('L', image.size, 255)
75 alpha.paste(image, mask=mask)
76 alpha = ImageOps.invert(alpha)
77 alpha = ImageOps.autocontrast(alpha)
78
79 return Image.merge('LA', [Image.new('L', image.size), alpha])
80
81 def filter_blend(image, baseDir, filename, opacity):
82 image, overlay = ensure_same_mode(
83 image,
84 load_image(os.path.join(
85 baseDir,
86 *filename.split('/')
87 ))
88 )
89
90 return Image.blend(image, overlay, float(opacity))
91
92 def convertImages(params, files):
93 metadata = params['metadata']
94
95 for filename, chain in metadata.items('convert_img'):
96 baseDir = os.path.dirname(metadata.option_source('convert_img', filename))
97 steps = re.split(r'\s*->\s*', chain)
98 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/')))
99
100 for step in steps:
101 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
102 args = re.split(r'\s*,\s*', args) if args else ()
103 image = globals()['filter_' + filter](image, baseDir, *args)
104
105 file = image_to_file(image, filename)
106 try:
107 files[filename] = file.read()
108 finally:
109 file.close()
OLDNEW
« no previous file with comments | « imageCompression.py ('k') | packagerChrome.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld