- Python Pillow Tutorial
- Python Pillow - Home
- Python Pillow - Overview
- Python Pillow - Environment Setup
- Basic Image Operations
- Python Pillow - Working with Images
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Adding Borders to Images
- Python Pillow - Identifying Image Files
- Python Pillow - Merging Images
- Python Pillow - Cutting and Pasting Images
- Python Pillow - Rolling an Image
- Python Pillow - Writing text on image
- Python Pillow - ImageDraw Module
- Python Pillow - Concatenating two Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Creating a Watermark
- Python Pillow - Image Sequences
- Python Pillow Color Conversions
- Python Pillow - Colors on an Image
- Python Pillow - Creating Images With Colors
- Python Pillow - Converting Color String to RGB Color Values
- Python Pillow - Converting Color String to Grayscale Values
- Python Pillow - Change the Color by Changing the Pixel Values
- Image Manipulation
- Python Pillow - Reducing Noise
- Python Pillow - Changing Image Modes
- Python Pillow - Compositing Images
- Python Pillow - Working with Alpha Channels
- Python Pillow - Applying Perspective Transforms
- Image Filtering
- Python Pillow - Adding Filters to an Image
- Python Pillow - Convolution Filters
- Python Pillow - Blur an Image
- Python Pillow - Edge Detection
- Python Pillow - Embossing Images
- Python Pillow - Enhancing Edges
- Python Pillow - Unsharp Mask Filter
- Image Enhancement and Correction
- Python Pillow - Enhancing Contrast
- Python Pillow - Enhancing Sharpness
- Python Pillow - Enhancing Color
- Python Pillow - Correcting Color Balance
- Python Pillow - Removing Noise
- Image Analysis
- Python Pillow - Extracting Image Metadata
- Python Pillow - Identifying Colors
- Advanced Topics
- Python Pillow - Creating Animated GIFs
- Python Pillow - Batch Processing Images
- Python Pillow - Converting Image File Formats
- Python Pillow - Adding Padding to an Image
- Python Pillow - Color Inversion
- Python Pillow - M L with Numpy
- Python Pillow with Tkinter BitmapImage and PhotoImage objects
- Image Module
- Python Pillow - Image Blending
- Python Pillow Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - Function Reference
- Python Pillow - Useful Resources
- Python Pillow - Discussion
Python Pillow - ImageOps.Colorize() Function
The PIL.ImageOps.colorize() function is used to colorize a grayscale image. It calculates a color wedge that maps all black pixels in the source image to the first color and all white pixels to the second color. If a midtone(mid) color is specified, it uses three-color mapping. The black and white parameters should be provided as RGB tuples or color names. Optionally, three-color mapping is possible by including the mid parameter. For each color, mapping positions can be defined using parameters such as blackpoint, representing an integer value indicating where the corresponding color should be mapped. It is essential to maintain a logical order for these parameters, ensuring that blackpoint is less than or equal to midpoint, and midpoint is less than or equal to whitepoint (if mid is specified).
Syntax
Following is the syntax of the function −
PIL.ImageOps.colorize(image, black, white, mid=None, blackpoint=0, whitepoint=255, midpoint=127)
Parameters
Here are the details of this function parameters −
image − The grayscale image to colorize.
black − The color to use for black input pixels, specified as an RGB tuple or color name.
white − The color to use for white input pixels, specified as an RGB tuple or color name.
mid − The color to use for midtone input pixels, specified as an RGB tuple or color name (optional for three-color mapping).
blackpoint − An integer value [0, 255] representing the mapping position for the black color.
whitepoint − An integer value [0, 255] representing the mapping position for the white color.
midpoint − An integer value [0, 255] representing the mapping position for the midtone color (optional for three-color mapping).
Return Value
The function returns an image representing the colorized result.
Examples
Example 1
Here's an example of how to use the ImageOps.colorize() function with basic parameters.
from PIL import Image, ImageOps # Open a grayscale image gray_image = Image.open("Images/flowers_1.jpg").convert("L") # Define colors for mapping red = (255, 0, 0) cyan = (0, 255, 255) # Apply colorize operation colorized_image = ImageOps.colorize(gray_image, black=red, white=cyan) # Display the original and colorized images gray_image.show() colorized_image.show()
Output
Input Image
Output Image
Example 2
This example demonstrates colorizing a grayscale image with optional three-color mapping.
from PIL import Image, ImageOps # Open a grayscale image gray_image = Image.open("Images/Car_2.jpg").convert("L") # Define colors for mapping black_color = 'yellow' white_color = 'red' mid_color = 'cyan' # color name (optional for three-color mapping) # Define mapping positions blackpoint = 50 whitepoint = 200 midpoint = 127 # Apply colorize operation colorized_image = ImageOps.colorize(gray_image, black_color, white_color, mid_color, blackpoint, whitepoint, midpoint) # Display the original and colorized images gray_image.show() colorized_image.show()
Output
Input Image
Output Image
Example 3
This example demonstrates how you can apply the colorize function with distinct colors and mapping positions to achieve a unique colorized result.
from PIL import Image, ImageOps # Open a grayscale image gray_image = Image.open("Images/Car_2.jpg").convert("L") # Define colors for mapping (RGB tuple) warm_black = (10, 10, 10) cool_white = (200, 220, 255) neutral_gray = (128, 128, 128) # Define mapping positions blackpoint = 30 whitepoint = 200 midpoint = 120 # Apply colorize operation with custom colors and mapping positions colorized_image = ImageOps.colorize(gray_image, warm_black, cool_white, neutral_gray, blackpoint, whitepoint, midpoint) # Display the original and colorized images gray_image.show() colorized_image.show()
Output
Input Image
Output Image