def equalize(image, amount=100): image = imtools.convert_safe_mode(image) if imtools.has_alpha(image): equalized = imtools.remove_alpha(image) else: equalized = image equalized = ImageOps.equalize(equalized) if imtools.has_alpha(image): imtools.put_alpha(equalized, imtools.get_alpha(image)) if amount < 100: equalized = imtools.blend(image, equalized, amount / 100.0) return equalized
def brightness(image, amount=50): """Adjust brightness from black to white - amount: -1(black) 0 (unchanged) 1(white) - repeat: how many times it should be repeated""" if amount == 0: return image image = imtools.convert_safe_mode(image) if amount < 0: #fade to black im = imtools.blend( image, Image.new(image.mode, image.size, 0), -amount / 100.0) else: #fade to white im = imtools.blend( image, Image.new(image.mode, image.size, ImageColor.getcolor('white', image.mode)), amount / 100.0) #fix image transparency mask if imtools.has_alpha(image): im.putalpha(imtools.get_alpha(image)) return im
def warmup(image, midtone, brighten, amount=100): """Apply a toning filter. Move the midtones to the desired color while preserving blacks and whites with optional mixing with original image - amount: 0-100%""" mode = image.mode info = image.info if image.mode != "L": im = imtools.convert(image, "L") else: im = image if image.mode != "RGBA" and imtools.has_transparency(image): image = imtools.convert(image, "RGBA") luma = imtools.convert(imtools.split(im)[0], "F") o = [] m = ImageColor.getrgb(midtone) b = brighten / 600.0 # Calculate channels separately for l in range(3): o.append(ImageMath.eval("m*(255-i)*i+i", i=luma, m=4 * ((m[l] / 255.0) - 0.5 + b) / 255.0).convert("L")) colorized = Image.merge("RGB", tuple(o)) if imtools.has_alpha(image): imtools.put_alpha(colorized, imtools.get_alpha(image)) if amount < 100: colorized = imtools.blend(image, colorized, amount / 100.0) return colorized
def invert(image, amount=100): image = imtools.convert_safe_mode(image) inverted = ImageChops.invert(image) if amount < 100: inverted = imtools.blend(image, inverted, amount / 100.0) if imtools.has_alpha(image): inverted.putalpha(imtools.get_alpha(image)) return inverted
def solarize(image, treshold, amount=100): """Apply a filter - amount: 0-1""" image = imtools.convert_safe_mode(image) solarized = image.convert('RGB') solarized = ImageOps.solarize(solarized, treshold) if imtools.has_alpha(image): imtools.put_alpha(solarized, imtools.get_alpha(image)) if amount < 100: return imtools.blend(image, solarized, amount / 100.0) return solarized
def posterize(image, bits, amount=100): """Apply a filter - amount: 0-1""" image = imtools.convert_safe_mode(image) posterized = imtools.remove_alpha(image) posterized = ImageOps.posterize(posterized, bits) if imtools.has_alpha(image): imtools.put_alpha(posterized, imtools.get_alpha(image)) if amount < 100: return imtools.blend(image, posterized, amount / 100.0) return posterized
def convert(self, mode, *args, **keyw): """Converts all layers to a different mode.""" for layer in self.layers.values(): if layer.image.mode == mode: continue if mode == 'P' and imtools.has_alpha(layer.image): layer.image = imtools.convert(layer.image, mode, *args, **keyw) self.info['transparency'] = 255 elif mode == 'P': layer.image = imtools.convert(layer.image, mode, *args, **keyw) self.info['transparency'] = None else: layer.image = imtools.convert(layer.image, mode, *args, **keyw)
def effect(image, filter, amount=100, repeat=1): """Apply a filter - amount: 0-1 - repeat: how many times it should be repeated""" filter = getattr(ImageFilter, filter) image = imtools.convert_safe_mode(image) for i in range(repeat): filtered = image.filter(filter) if imtools.has_alpha(image) and \ filter in [ImageFilter.CONTOUR, ImageFilter.EMBOSS]: filtered.putalpha(imtools.get_alpha(image)) if amount < 100: image = imtools.blend(image, filtered, amount / 100.0) else: image = filtered return image
def reflect(image, depth, opacity, background_color, background_opacity, scale_method, gap=0, scale_reflection=False, blur_reflection=False, cache=None): if has_transparency(image): image = image.convert('RGBA') else: image = image.convert('RGB') if cache is None: cache = {} opacity = (255 * opacity) / 100 background_opacity = (255 * background_opacity) / 100 scale_method = getattr(Image, scale_method) if background_opacity == 255: mode = 'RGB' color = background_color else: mode = 'RGBA' color = HTMLColorToRGBA(background_color, background_opacity) width, height = image.size depth = min(height, depth) #make reflection if has_alpha(image) and background_opacity > 0: reflection = Image.new(mode, image.size, color) paste(reflection, image, (0, 0), image) else: reflection = image reflection = reflection.transpose(Image.FLIP_TOP_BOTTOM) if scale_reflection: reflection = reflection.resize((width, depth), scale_method) else: reflection = reflection.crop((0, 0, width, depth)) if blur_reflection: reflection = reflection.filter(ImageFilter.BLUR) mask = gradient_mask((width, depth), opacity, cache) #composite total_size = (width, height + gap + depth) total = Image.new(mode, total_size, color) paste(total, image, (0, 0), image) paste(total, reflection, (0, height + gap), mask) return total
def warmup(image, midtone, brighten, amount=100): """Apply a toning filter. Move the midtones to the desired color while preserving blacks and whites with optional mixing with original image - amount: 0-100%""" mode = image.mode info = image.info if image.mode != 'L': im = imtools.convert(image, 'L') else: im = image if image.mode != 'RGBA' and imtools.has_transparency(image): image = imtools.convert(image, 'RGBA') luma = imtools.convert(imtools.split(im)[0], 'F') o = [] m = ImageColor.getrgb(midtone) b = brighten / 600.0 # Calculate channels separately for l in range(3): o.append( ImageMath.eval("m*(255-i)*i+i", i=luma, m=4 * ((m[l] / 255.0) - 0.5 + b) / 255.0).convert('L')) colorized = Image.merge('RGB', tuple(o)) if imtools.has_alpha(image): imtools.put_alpha(colorized, imtools.get_alpha(image)) if amount < 100: colorized = imtools.blend(image, colorized, amount / 100.0) return colorized
def brightness(image, amount=50): """Adjust brightness from black to white - amount: -1(black) 0 (unchanged) 1(white) - repeat: how many times it should be repeated""" if amount == 0: return image image = imtools.convert_safe_mode(image) if amount < 0: #fade to black im = imtools.blend(image, Image.new(image.mode, image.size, 0), -amount / 100.0) else: #fade to white im = imtools.blend( image, Image.new(image.mode, image.size, ImageColor.getcolor('white', image.mode)), amount / 100.0) #fix image transparency mask if imtools.has_alpha(image): im.putalpha(imtools.get_alpha(image)) return im
def make_grid(image, grid, col_line_width=0, row_line_width=0, line_color='#FFFFFF', line_opacity=0, old_size=None, scale=True): # Check if there is any work to do. if grid == (1, 1): return image # Because of layer support photo size can be different # from image layer size if old_size is None: old_size = image.size # Unpack grid cols, rows = grid # Scaling down? if scale: # Keep the same number of pixels in the result s = sqrt(cols * rows) old_size = tuple(map(lambda x: int(x / s), old_size)) # To scale down we need to make the image processing safe. image = imtools.convert_safe_mode(image)\ .resize(old_size, getattr(Image, 'ANTIALIAS')) #displacement dx, dy = old_size dx += col_line_width dy += row_line_width new_size = cols * dx - col_line_width, rows * dy - row_line_width # The main priority is that the new_canvas has the same mode as the image. # Palette images if image.mode == 'P': if 0 < line_opacity < 255: # transparent lines require RGBA image = imtools.convert(image, 'RGBA') else: if 'transparency' in image.info and line_opacity == 0: # Make line color transparent for images # with transparency. line_color_index = image.info['transparency'] palette = None else: line_color_index, palette = imtools.fit_color_in_palette( image, ImageColor.getrgb(line_color), ) if line_color_index != -1: new_canvas = Image.new('P', new_size, line_color_index) imtools.put_palette(new_canvas, image, palette) else: # Convert to non palette image (RGB or RGBA) image = imtools.convert_safe_mode(image) # Non palette images if image.mode != 'P': line_color = ImageColor.getcolor(line_color, image.mode) if imtools.has_alpha(image): # Make line color transparent for images # with an alpha channel. line_color = tuple(list(line_color)[:-1] + [line_opacity]) pass new_canvas = Image.new(image.mode, new_size, line_color) # Paste grid for x in range(cols): for y in range(rows): pos = (x * dx, y * dy) imtools.paste(new_canvas, image, pos, force=True) return new_canvas