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 __init__(self, text, font=None, text_color=None, **kwargs): # fill in base defaults defaults = dict(opacity=0.5) defaults.update(kwargs) self._fill_options(**defaults) # fill in specific settings self.text = text if isinstance(font, str): # `font` is a path to a font fontpath = font fontsize = 16 elif isinstance(font, tuple): # `font` is a (path, size) tuple fontpath = font[0] fontsize = font[1] elif font is not None: raise TypeError("Expected 'font' to be a path to a font file " "or a tuple containing (path, size).") try: if fontpath.endswith(".pil"): # bitmap font (size doesn't matter) self.font = ImageFont.load(fontpath) else: # truetype font (size matters) self.font = ImageFont.truetype(fontpath, fontsize) except: warnings.warn("The specified font '%s' could not be loaded" % (font,), RuntimeWarning) font = None if not font: self.font = ImageFont.load_default() if text_color is None: # fallback to default self.text_color = (255, 255, 255) elif isinstance(text_color, str): # string in a form ImageColor module can process self.text_color = ImageColor.getrgb(text_color) elif isinstance(text_color, tuple): # tuple with (R,G,B) # if it has (R,G,B,A), the alpha component seems to be ignored by PIL # when rendering text self.text_color = text_color else: raise TypeError("Expected `text_color` to be tuple or string.") self.font_size = self.font.getsize(text)
def process(cls, img, fmt, obj): if cls.width and cls.height: background_color = ImageColor.getrgb(cls.background_color) #FIXME : Image is not imported but it never raises exception so ... bg_picture = Image.new("RGB", (cls.width, cls.height), background_color) ## paste it bg_w, bg_h = bg_picture.size img_w, img_h = img.size coord_x, coord_y = (bg_w - img_w) / 2, (bg_h - img_h) / 2 bg_picture.paste(img, (coord_x, coord_y, coord_x + img_w, coord_y + img_h)) return bg_picture, fmt
def process(self, img): # Convert bgcolor string to RGB value. background_color = ImageColor.getrgb(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 * 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("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])) # Return the image complete with reflection effect. return composite
def _text_color(self): return ImageColor.getrgb(self.font_color)