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

Side by Side Diff: imageConversion.py

Issue 11544056: Prepared buildtools for Safari (Closed)
Patch Set: Created Sept. 19, 2013, 2:26 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 | « build.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 file is part of the Adblock Plus build tools,
4 # Copyright (C) 2006-2013 Eyeo GmbH
5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17
18 import os
19 import re
20 from StringIO import StringIO
21
22 import PIL.Image
23 import PIL.ImageOps
24
25 def get_alpha(image):
26 if image.mode in ('RGBA', 'LA'):
27 return image.split()[image.getbands().index('A')]
28
29 # images with palette doesn't have an alpha channel, but
30 # can store transparent pixels by using 0 as image data.
31 # In order to don't discard those information, we create an
32 # alpha channel from all the pixels that are not transparent.
33 if image.mode == 'P':
34 return image.point([0] + [255] * 255, 'L')
35
36 def ensure_same_mode(im1, im2):
37 # if both images already have the same mode (and palette, in
38 # case of mode P), don't convert anything. Images with mode P,
39 # and a different palette, are the only case where images
40 # using the same mode, will be incompatible with each other.
41 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal ette()):
42 return (im1, im2)
43
44 # if any given image has a mode that supports colors convert both
45 # images to RGB or RGBA, otherwise convert both images to L or LA.
46 # If any given image has an alpha channel (or mode P which
47 # can store transparent pixels too) convert both images
48 # to RGBA or LA, otherwise convert both images to RGB or L.
49 mode = max(
50 PIL.Image.getmodebase(im1.mode),
51 PIL.Image.getmodebase(im2.mode),
52
53 key=('L', 'RGB').index
54 )
55
56 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)):
57 mode += 'A'
58
59 return (
60 im1 if im1.mode == mode else im1.convert(mode),
61 im2 if im2.mode == mode else im2.convert(mode),
62 )
63
64 def filter_grayscale(image, baseDir):
65 alpha = get_alpha(image)
66 image = image.convert('L')
67
68 if alpha:
69 image.putalpha(alpha)
70
71 return image
72
73 def filter_contrastToAlpha(image, baseDir):
74 alpha = PIL.Image.new('L', image.size, 255)
75 alpha.paste(image, mask=get_alpha(image))
76 alpha = PIL.ImageOps.invert(alpha)
77 alpha = PIL.ImageOps.autocontrast(alpha)
78
79 return PIL.Image.merge('LA', [PIL.Image.new('L', image.size), alpha])
80
81 def filter_blend(image, baseDir, *args):
82 if len(args) == 2:
83 filename, opacity = args
84
85 overlay = PIL.Image.open(os.path.join(
86 baseDir,
87 *filename.split('/')
88 ))
89 else:
90 red, green, blue, opacity = args
91
92 overlay = PIL.Image.new('RGB', image.size, (
93 int(red),
94 int(green),
95 int(blue),
96 ))
97
98 # if the background image has an alpha channel copy it to
99 # the overlay, so that transparent areas stay transparent.
100 alpha = get_alpha(image)
101 if alpha:
102 overlay.putalpha(alpha)
103
104 image, overlay = ensure_same_mode(image, overlay)
105 return PIL.Image.blend(image, overlay, float(opacity))
106
107 def convertImages(params, files):
108 metadata = params['metadata']
109
110 for filename, chain in metadata.items('convert_img'):
111 baseDir = os.path.dirname(metadata.option_source('convert_img', filename))
112 steps = re.split(r'\s*->\s*', chain)
113 image = PIL.Image.open(os.path.join(baseDir, *steps.pop(0).split('/')))
114
115 for step in steps:
116 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
117 args = re.split(r'\s*,\s*', args) if args else ()
118 image = globals()['filter_' + filter](image, baseDir, *args)
119
120 f = StringIO()
121 f.name = filename
122 image.save(f)
123 files[filename] = f.getvalue()
OLDNEW
« no previous file with comments | « build.py ('k') | packagerChrome.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld