def process(self, img, config, info): if self.key not in config: return img # convert bgcolor string to rgb value config = config[self.key] background_color = ImageColor.getrgb( config.get('background_color', self.background_color)) # handle palleted images img = img.convert('RGB') # 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("RGB", img.size, background_color) # calculate our alpha mask start = int( 255 - (255 * config.get('opacity', self.opacity))) # The start of our gradient steps = int( 255 * config.get('size', 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] * config.get('size', 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("RGB", (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])) # Save the file as a JPEG info['format'] = 'JPEG' # return the image complete with reflection effect return composite
def process(self, img, config, info): if self.key not in config: return img bw = img.convert('1') bw = bw.filter(ImageFilter.MedianFilter) # White background. bg = Image.new('1', img.size, 255) diff = ImageChops.difference(bw, bg) bbox = diff.getbbox() if bbox: img = img.crop(bbox) return img
def process(self, img, config, info): if self.key not in config: return img # convert bgcolor string to rgb value config = config[self.key] background_color = ImageColor.getrgb(config.get('background_color', self.background_color)) # handle palleted images img = img.convert('RGB') # 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("RGB", img.size, background_color) # calculate our alpha mask start = int(255 - (255 * config.get('opacity', self.opacity))) # The start of our gradient steps = int(255 * config.get('size', 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] * config.get('size', 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("RGB", (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])) # Save the file as a JPEG info['format'] = 'JPEG' # return the image complete with reflection effect return composite