Created
May 18, 2012 20:58
-
-
Save bennylope/2727580 to your computer and use it in GitHub Desktop.
Quick example of creating a recolorized (one-color) image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Image | |
flowers = Image.open("flowers.jpg") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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