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

Delta Between Two Patch Sets: imageConversion.py

Issue 4826241328742400: Made import of PIL compatible to environments where you have to import Image directly (Closed)
Left Patch Set: Created Nov. 15, 2013, 9:36 a.m.
Right Patch Set: Created Nov. 15, 2013, 9:40 a.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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 # using the same mode, will be incompatible with each other. 46 # 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()): 47 if im1.mode == im2.mode and (im1.mode != 'P' or im1.getpalette() == im2.getpal ette()):
48 return (im1, im2) 48 return (im1, im2)
49 49
50 # if any given image has a mode that supports colors convert both 50 # if any given image has a mode that supports colors convert both
51 # images to RGB or RGBA, otherwise convert both images to L or LA. 51 # images to RGB or RGBA, otherwise convert both images to L or LA.
52 # If any given image has an alpha channel (or mode P which 52 # If any given image has an alpha channel (or mode P which
53 # can store transparent pixels too) convert both images 53 # can store transparent pixels too) convert both images
54 # to RGBA or LA, otherwise convert both images to RGB or L. 54 # to RGBA or LA, otherwise convert both images to RGB or L.
55 mode = max( 55 mode = max(
56 PIL.Image.getmodebase(im1.mode), 56 Image.getmodebase(im1.mode),
57 PIL.Image.getmodebase(im2.mode), 57 Image.getmodebase(im2.mode),
58 58
59 key=('L', 'RGB').index 59 key=('L', 'RGB').index
60 ) 60 )
61 61
62 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)): 62 if any(im.mode in ('RGBA', 'LA', 'P') for im in (im1, im2)):
63 mode += 'A' 63 mode += 'A'
64 64
65 return ( 65 return (
66 im1 if im1.mode == mode else im1.convert(mode), 66 im1 if im1.mode == mode else im1.convert(mode),
67 im2 if im2.mode == mode else im2.convert(mode), 67 im2 if im2.mode == mode else im2.convert(mode),
68 ) 68 )
69 69
70 def filter_grayscale(image, baseDir): 70 def filter_grayscale(image, baseDir):
71 alpha = get_alpha(image) 71 alpha = get_alpha(image)
72 image = image.convert('L') 72 image = image.convert('L')
73 73
74 if alpha: 74 if alpha:
75 image.putalpha(alpha) 75 image.putalpha(alpha)
76 76
77 return image 77 return image
78 78
79 def filter_contrastToAlpha(image, baseDir): 79 def filter_contrastToAlpha(image, baseDir):
80 alpha = PIL.Image.new('L', image.size, 255) 80 alpha = Image.new('L', image.size, 255)
81 alpha.paste(image, mask=get_alpha(image)) 81 alpha.paste(image, mask=get_alpha(image))
82 alpha = PIL.ImageOps.invert(alpha) 82 alpha = ImageOps.invert(alpha)
83 alpha = PIL.ImageOps.autocontrast(alpha) 83 alpha = ImageOps.autocontrast(alpha)
84 84
85 return PIL.Image.merge('LA', [PIL.Image.new('L', image.size), alpha]) 85 return Image.merge('LA', [Image.new('L', image.size), alpha])
86 86
87 def filter_blend(image, baseDir, *args): 87 def filter_blend(image, baseDir, *args):
88 if len(args) == 2: 88 if len(args) == 2:
89 filename, opacity = args 89 filename, opacity = args
90 90
91 overlay = PIL.Image.open(os.path.join( 91 overlay = Image.open(os.path.join(
92 baseDir, 92 baseDir,
93 *filename.split('/') 93 *filename.split('/')
94 )) 94 ))
95 else: 95 else:
96 red, green, blue, opacity = args 96 red, green, blue, opacity = args
97 97
98 overlay = PIL.Image.new('RGB', image.size, ( 98 overlay = Image.new('RGB', image.size, (
99 int(red), 99 int(red),
100 int(green), 100 int(green),
101 int(blue), 101 int(blue),
102 )) 102 ))
103 103
104 # if the background image has an alpha channel copy it to 104 # if the background image has an alpha channel copy it to
105 # the overlay, so that transparent areas stay transparent. 105 # the overlay, so that transparent areas stay transparent.
106 alpha = get_alpha(image) 106 alpha = get_alpha(image)
107 if alpha: 107 if alpha:
108 overlay.putalpha(alpha) 108 overlay.putalpha(alpha)
109 109
110 image, overlay = ensure_same_mode(image, overlay) 110 image, overlay = ensure_same_mode(image, overlay)
111 return PIL.Image.blend(image, overlay, float(opacity)) 111 return Image.blend(image, overlay, float(opacity))
112 112
113 def convertImages(params, files): 113 def convertImages(params, files):
114 metadata = params['metadata'] 114 metadata = params['metadata']
115 115
116 for filename, chain in metadata.items('convert_img'): 116 for filename, chain in metadata.items('convert_img'):
117 baseDir = os.path.dirname(metadata.option_source('convert_img', filename)) 117 baseDir = os.path.dirname(metadata.option_source('convert_img', filename))
118 steps = re.split(r'\s*->\s*', chain) 118 steps = re.split(r'\s*->\s*', chain)
119 image = PIL.Image.open(os.path.join(baseDir, *steps.pop(0).split('/'))) 119 image = Image.open(os.path.join(baseDir, *steps.pop(0).split('/')))
120 120
121 for step in steps: 121 for step in steps:
122 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups() 122 filter, args = re.match(r'([^(]+)(?:\((.*)\))?', step).groups()
123 args = re.split(r'\s*,\s*', args) if args else () 123 args = re.split(r'\s*,\s*', args) if args else ()
124 image = globals()['filter_' + filter](image, baseDir, *args) 124 image = globals()['filter_' + filter](image, baseDir, *args)
125 125
126 f = StringIO() 126 f = StringIO()
127 f.name = filename 127 f.name = filename
128 image.save(f) 128 image.save(f)
129 files[filename] = f.getvalue() 129 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