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

Delta Between Two Patch Sets: imageConversion.py

Issue 6099042538881024: Wait until images are loaded (Closed)
Left Patch Set: Created Nov. 16, 2013, 2 p.m.
Right Patch Set: Also make sure the image is loaded in filter_blend(), add a comment Created Nov. 16, 2013, 3:10 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 | « no previous file | no next file » | 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 1 # coding: utf-8
2 2
3 # This file is part of the Adblock Plus build tools, 3 # This file is part of the Adblock Plus build tools,
4 # Copyright (C) 2006-2013 Eyeo GmbH 4 # Copyright (C) 2006-2013 Eyeo GmbH
5 # 5 #
6 # Adblock Plus is free software: you can redistribute it and/or modify 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 7 # it under the terms of the GNU General Public License version 3 as
8 # published by the Free Software Foundation. 8 # published by the Free Software Foundation.
9 # 9 #
10 # Adblock Plus is distributed in the hope that it will be useful, 10 # Adblock Plus is distributed in the hope that it will be useful,
(...skipping 20 matching lines...) Expand all
31 return image.split()[image.getbands().index('A')] 31 return image.split()[image.getbands().index('A')]
32 32
33 if image.mode == 'P': 33 if image.mode == 'P':
34 transparency = image.info.get('transparency') 34 transparency = image.info.get('transparency')
35 35
36 if transparency is not None: 36 if transparency is not None:
37 table = [255] * 256 37 table = [255] * 256
38 table[transparency] = 0 38 table[transparency] = 0
39 39
40 return image.point(table, 'L') 40 return image.point(table, 'L')
41
42 def load_image(path):
43 image = Image.open(path)
44 # Make sure the image is loaded, some versions of PIL load images lazily.
45 image.load()
46 return image
41 47
42 def ensure_same_mode(im1, im2): 48 def ensure_same_mode(im1, im2):
43 # if both images already have the same mode (and palette, in 49 # if both images already have the same mode (and palette, in
44 # case of mode P), don't convert anything. Images with mode P, 50 # case of mode P), don't convert anything. Images with mode P,
45 # and a different palette, are the only case where images 51 # and a different palette, are the only case where images
46 # using the same mode, will be incompatible with each other. 52 # using the same mode, will be incompatible with each other.
47 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal ette()): 53 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal ette()):
48 return (im1, im2) 54 return (im1, im2)
49 55
50 # if any given image has a mode that supports colors convert both 56 # if any given image has a mode that supports colors convert both
(...skipping 30 matching lines...) Expand all
81 alpha.paste(image, mask=get_alpha(image)) 87 alpha.paste(image, mask=get_alpha(image))
82 alpha = ImageOps.invert(alpha) 88 alpha = ImageOps.invert(alpha)
83 alpha = ImageOps.autocontrast(alpha) 89 alpha = ImageOps.autocontrast(alpha)
84 90
85 return Image.merge('LA', [Image.new('L', image.size), alpha]) 91 return Image.merge('LA', [Image.new('L', image.size), alpha])
86 92
87 def filter_blend(image, baseDir, *args): 93 def filter_blend(image, baseDir, *args):
88 if len(args) == 2: 94 if len(args) == 2:
89 filename, opacity = args 95 filename, opacity = args
90 96
91 overlay = Image.open(os.path.join( 97 overlay = load_image(os.path.join(
92 baseDir, 98 baseDir,
93 *filename.split('/') 99 *filename.split('/')
94 )) 100 ))
95 else: 101 else:
96 red, green, blue, opacity = args 102 red, green, blue, opacity = args
97 103
98 overlay = Image.new('RGB', image.size, ( 104 overlay = Image.new('RGB', image.size, (
99 int(red), 105 int(red),
100 int(green), 106 int(green),
101 int(blue), 107 int(blue),
102 )) 108 ))
103 109
104 # if the background image has an alpha channel copy it to 110 # if the background image has an alpha channel copy it to
105 # the overlay, so that transparent areas stay transparent. 111 # the overlay, so that transparent areas stay transparent.
106 alpha = get_alpha(image) 112 alpha = get_alpha(image)
107 if alpha: 113 if alpha:
108 overlay.putalpha(alpha) 114 overlay.putalpha(alpha)
109 115
110 image, overlay = ensure_same_mode(image, overlay) 116 image, overlay = ensure_same_mode(image, overlay)
111 return Image.blend(image, overlay, float(opacity)) 117 return Image.blend(image, overlay, float(opacity))
112 118
113 def convertImages(params, files): 119 def convertImages(params, files):
114 metadata = params['metadata'] 120 metadata = params['metadata']
115 121
116 for filename, chain in metadata.items('convert_img'): 122 for filename, chain in metadata.items('convert_img'):
117 baseDir = os.path.dirname(metadata.option_source('convert_img', filename)) 123 baseDir = os.path.dirname(metadata.option_source('convert_img', filename))
118 steps = re.split(r'\s*->\s*', chain) 124 steps = re.split(r'\s*->\s*', chain)
119 image = Image.open(os.path.join(baseDir, *steps.pop(0).split('/'))) 125 image = load_image(os.path.join(baseDir, *steps.pop(0).split('/')))
120 image.load()
121 126
122 for step in steps: 127 for step in steps:
123 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() 128 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
124 args = re.split(r'\s*,\s*', args) if args else () 129 args = re.split(r'\s*,\s*', args) if args else ()
125 image = globals()['filter_' + filter](image, baseDir, *args) 130 image = globals()['filter_' + filter](image, baseDir, *args)
126 131
127 f = StringIO() 132 f = StringIO()
128 f.name = filename 133 f.name = filename
129 image.save(f) 134 image.save(f)
130 files[filename] = f.getvalue() 135 files[filename] = f.getvalue()
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld