def process(cls, image, fmt, obj=None): """Adds a watermark to an image.""" if image.mode != 'RGBA': image = image.convert('RGBA') pink = (255, 94, 200) white = (255, 255, 255) im_width, im_height = image.size copyright = "Copyright \xa9 by the Artist and Burning Man" overlay = Image.new('RGBA', image.size, (0,0,0,0)) draw = ImageDraw.Draw(overlay) draw.rectangle((0, im_height - 20, im_width, im_height), fill=(0,0,0,90)) draw.text((10, im_height - 15), copyright, fill=(255,255,255,90)) newimage = Image.new('RGB', (im_width, im_height + 50), white) draw2 = ImageDraw.Draw(newimage) draw2.text((10, im_height + 5), "\xa9 All images are copyright in their respective year, by " "both the ", fill=pink) draw2.text((10, im_height + 15), "photographer and Burning Man. For publication or other use ", fill=pink) draw2.text((10, im_height + 25), "requests, contact the photographer at the email provided and ", fill=pink) draw2.text((10, im_height + 35), "[email protected] for written permission.", fill=pink) comp = Image.composite(overlay, image, overlay) newimage.paste(comp, (0,0)) return newimage, fmt
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 process(self, image): mask = Image.open(os.path.join(settings.STATIC_ROOT, 'img/avatar-mask.png')) layer = Image.new('RGBA', image.size) layer.paste(mask) newImage = Image.composite(layer, image, layer) return newImage
def process(self, img): # get watermark wm = self._get_watermark() wm_size = wm.size # print('wm', wm) # print('wm.mode', wm.mode) # from PIL.PngImagePlugin import PngImageFile # from PIL.JpegImagePlugin import JpegImageFile if self.scale: if isinstance(self.scale, (int, float)) and self.scale != 1: # L&X # wm_size[0] *= self.scale # wm_size[1] *= self.scale wm_size = (wm_size[0] * self.scale, wm_size[1] * self.scale) wm = wm.scale(wm_size) elif self.scale == True: # from .resize import ResizeToFit from imagekit.processors import ResizeToFit wm = ResizeToFit(width=img.size[0], height=img.size[1], upscale=True).process(wm) wm_size = wm.size # prepare image for overlaying (ensure alpha channel) if img.mode != 'RGBA': img = img.convert('RGBA') # create a layer to place the watermark layer = Image.new('RGBA', img.size, (0, 0, 0, 0)) coords = _process_coords(img.size, wm_size, self.position) print('L&X', 'wm', wm) print('L&X', 'wm.size', wm.size) print('L&X', 'coords', coords) coords = (int(coords[0]), int(coords[1])) if self.repeat: sx = coords[0] % wm_size[0] - wm_size[0] sy = coords[1] % wm_size[1] - wm_size[1] for x in range(sx, img.size[0], wm_size[0]): for y in range(sy, img.size[1], wm_size[1]): layer.paste(wm, (x, y)) else: layer.paste(wm, coords) if self.opacity < 1: alpha = layer.split()[3] alpha = ImageEnhance.Brightness(alpha).enhance(self.opacity) layer.putalpha(alpha) # merge watermark layer img = Image.composite(layer, img, layer) return img
def process(self, img): # get watermark wm = self._get_watermark() wm_size = wm.size if self.scale: if isinstance(self.scale, (int, float)) and self.scale != 1: wm_size[0] *= self.scale wm_size[1] *= self.scale wm = wm.scale(wm_size) elif self.scale == True: from imagekit.processors import ResizeToFit wm = ResizeToFit(width=img.size[0], height=img.size[1], upscale=True).process(wm) wm_size = wm.size # prepare image for overlaying (ensure alpha channel) if img.mode != 'RGBA': img = img.convert('RGBA') # create a layer to place the watermark layer = Image.new('RGBA', img.size, (0, 0, 0, 0)) coords = _process_coords(img.size, wm_size, self.position) if self.repeat: sx = coords[0] % wm_size[0] - wm_size[0] sy = coords[1] % wm_size[1] - wm_size[1] for x in range(sx, img.size[0], wm_size[0]): for y in range(sy, img.size[1], wm_size[1]): layer.paste(wm, (x, y)) else: layer.paste(wm, coords) if self.opacity < 1: alpha = layer.split()[3] alpha = ImageEnhance.Brightness(alpha).enhance(self.opacity) layer.putalpha(alpha) # merge watermark layer img = Image.composite(layer, img, layer) return img
def process(self, img): # get watermark wm = self._get_watermark() wm_size = wm.size if self.scale: if isinstance(self.scale, (int, float)) and self.scale != 1: wm_size[0] *= self.scale wm_size[1] *= self.scale wm = wm.scale(wm_size) elif self.scale == True: wm = ResizeToFit(width=img.size[0], height=img.size[1], upscale=True).process(wm) wm_size = wm.size # prepare image for overlaying (ensure alpha channel) if img.mode != 'RGBA': img = img.convert('RGBA') # create a layer to place the watermark layer = Image.new('RGBA', img.size, (0,0,0,0)) coords = _process_coords(img.size, wm_size, self.position) if self.repeat: sx = coords[0] % wm_size[0] - wm_size[0] sy = coords[1] % wm_size[1] - wm_size[1] for x in range(sx, img.size[0], wm_size[0]): for y in range(sy, img.size[1], wm_size[1]): layer.paste(wm, (x,y)) else: layer.paste(wm, coords) if self.opacity < 1: alpha = layer.split()[3] alpha = ImageEnhance.Brightness(alpha).enhance(self.opacity) layer.putalpha(alpha) # merge watermark layer img = Image.composite(layer, img, layer) return img
def process(self, img): # get watermark wm = Image.open(self.watermark_path) wm_size = wm.size # prepare image for overlaying (ensure alpha channel) if img.mode != 'RGBA': img = img.convert('RGBA') # create a layer to place the watermark layer = Image.new('RGBA', img.size, (0, 0, 0, 0)) coords = (img.size[0] - wm_size[0] - 20, img.size[1] - wm_size[1] - 20) layer.paste(wm, coords) img = Image.composite(layer, img, layer) return img
def process(self, img_path): src_img = Image.open(img_path) wm = self._get_watermark() wm_size = list(wm.size) if self.scale: wm = ResizeToFit(src_img.size[0], src_img.size[1], True).process(wm) wm_size = wm.size # # prepare image for overlaying (ensure alpha channel) # if src_img.mode != 'RGBA': # src_img = src_img.convert('RGBA') # create a layer to place the watermark layer = Image.new('RGBA', src_img.size, (0, 0, 0, 0)) coordinates = self._process_coordinates(src_img.size, wm_size) if self.repeat: sx = coordinates[0] % wm_size[0] - wm_size[0] sy = coordinates[1] % wm_size[1] - wm_size[1] for x in range(sx, src_img.size[0], wm_size[0]): for y in range(sy, src_img.size[1], wm_size[1]): layer.paste(wm, (x, y)) else: layer.paste(wm, coordinates) if self.opacity < 1: alpha = layer.split()[3] alpha = ImageEnhance.Brightness(alpha).enhance(self.opacity) layer.putalpha(alpha) # merge watermark layer dst_img = Image.composite(layer, src_img, layer) os.remove(img_path) dst_img.save(img_path)
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 add_watermark(image, footer=True, extended=True): if image.mode != 'RGBA': image = image.convert('RGBA') pink = (255, 94, 200) white = (255, 255, 255) im_width, im_height = image.size log.debug('size: %s', image.size) if im_height: fontsize = int(im_height/34) else: fontsize = int(im_width/34) if fontsize > 16: fontsize = 16 log.debug('font size=%i', fontsize) ttf = os.path.join(settings.MEDIA_ROOT, 'fonts', 'Tahoma.ttf') font = ImageFont.truetype(ttf, fontsize) overlay = Image.new('RGBA', image.size, (0,0,0,0)) if footer: copyright = "Copyright \xa9 by the Artist and Burning Man" draw = ImageDraw.Draw(overlay) draw.rectangle((0, im_height - fontsize - 6, im_width, im_height), fill=(0,0,0,90)) draw.text((10, im_height - fontsize - 4), copyright, fill=(255,255,255,90), font=font) if extended: newimage = Image.new('RGB', (im_width, im_height + 200), white) textheight = draw_word_wrap(newimage, EXTRA_COPY, 10, 5, max_width=im_width-10, fill=pink, font=font, height_only=True) h = im_height + textheight + 5 log.debug('new height: %s, textheight=%s', h, textheight) newimage = Image.new('RGB', (im_width, h), white) else: newimage = Image.new('RGB', (im_width, im_height)) if extended: #draw2 = ImageDraw.Draw(newimage) draw_word_wrap(newimage, EXTRA_COPY, 10, im_height + 5, max_width=im_width-10, fill=pink, font=font) # draw2.text((10, h), # "\xa9 All images are copyright in their respective year, by " # "both the ", # fill=pink, # font=font) # h += fontsize + 3 # draw2.text((10, h), # "photographer and Burning Man. For publication or other use ", # fill=pink, # font=font) # h += fontsize + 3 # draw2.text((10, h), # "requests, contact the photographer at the email provided and ", # fill=pink, # font=font) # h += fontsize + 3 # draw2.text((10, h), # "[email protected] for written permission.", # fill=pink, # font=font) comp = Image.composite(overlay, image, overlay) newimage.paste(comp, (0,0)) return newimage