Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick example of creating a recolorized (one-color) image
# Replacement for ImageFilter.GaussianBlur which always sets the
# blur radius to the default of 2.
# Code from http://aaronfay.ca/content/post/python-pil-and-gaussian-blur/
import ImageFilter
class MyGaussianBlur(ImageFilter.Filter):
name = "GaussianBlur"
def __init__(self, radius=2):
self.radius = radius
def filter(self, image):
return image.gaussian_blur(self.radius)
import Image
flowers = Image.open("flowers.jpg")
import Image
import ImageEnhance
def image_overlay(src, color="#FFFFFF", alpha=0.5):
"""
Returns a washed out, one color version of the initial image as an Image
object.
It does this with a really simple hack: set the original image to B&W and
then drop an alpha valued color layer on top. Perfect? No. Works? Yes. You
can experiment by changing the order of the overlay below to see what works
best.
color: optional color value of the overlay; can be provided in any format
acceptable to PIL.
alpha: optional alpha value for the overlay.
"""
overlay = Image.new(src.mode, src.size, color)
bw_src = ImageEnhance.Color(src).enhance(0.0)
return Image.blend(bw_src, overlay, alpha)
import Image
import ImageOps
def image_recolorize(src, black="#000099", white="#99CCFF"):
"""
Returns a recolorized version of the initial image using a two-tone
approach. The color in the black argument is used to replace black pixels
and the color in the white argument is used to replace white pixels.
The defaults set the image to a blue hued image.
"""
return ImageOps.colorize(ImageOps.grayscale(src), black, white)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment