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 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 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 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 autocontrast(image, amount=100.0, cutoff=0): """Apply a filter - amount: 0-1 - repeat: how many times it should be repeated""" image = imtools.convert_safe_mode(image) if imtools.has_transparency(image): im = imtools.remove_alpha(image) else: im = image contrasted = ImageOps.autocontrast(im, cutoff) if imtools.has_transparency(image): imtools.put_alpha(contrasted, imtools.get_alpha(image)) if amount < 100: return imtools.blend(image, contrasted, amount / 100.0) return contrasted
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