def process(self, img): # Convert bgcolor string to RGB value. background_color = ImageColor.getrgb(self.background_color) # Handle palleted images. img = img.convert('RGBA') # Copy orignial image and flip the orientation. reflection = img.copy().transpose(Image.FLIP_TOP_BOTTOM) # Create a new image filled with the bgcolor the same size. background = Image.new("RGBA", img.size, background_color) # Calculate our alpha mask. start = int(255 - (255 * self.opacity)) # The start of our gradient. steps = int(255 * self.size) # The number of intermedite values. increment = (255 - start) / float(steps) mask = Image.new('L', (1, 255)) for y in range(255): if y < steps: val = int(y * increment + start) else: val = 255 mask.putpixel((0, y), val) alpha_mask = mask.resize(img.size) # Merge the reflection onto our background color using the alpha mask. reflection = Image.composite(background, reflection, alpha_mask) # Crop the reflection. reflection_height = int(img.size[1] * self.size) reflection = reflection.crop((0, 0, img.size[0], reflection_height)) # Create new image sized to hold both the original image and # the reflection. composite = Image.new("RGBA", (img.size[0], img.size[1] + reflection_height), background_color) # Paste the orignal image and the reflection into the composite image. composite.paste(img, (0, 0)) composite.paste(reflection, (0, img.size[1])) # Return the image complete with reflection effect. return composite
def test_coloroverlay(): """ Test that the ColorOverlay processor """ img = Image.new('RGB', (200, 100)) color = ImageColor.getrgb('#cc0000') img = ColorOverlay(color, overlay_opacity=1.0).process(img) eq_(img.getpixel((0,0)), (204, 0, 0))
def test_coloroverlay(): """ Test that the ColorOverlay processor """ img = Image.new('RGB', (200, 100)) color = ImageColor.getrgb('#cc0000') img = ColorOverlay(color, overlay_opacity=1.0).process(img) eq_(img.getpixel((0, 0)), (204, 0, 0))