Basic Image Operations

Python Pillow Color Conversions

Image Manipulation

Image Filtering

Image Enhancement and Correction

Image Analysis

Advanced Topics

  • Image Module
  • Python Pillow Useful Resources

    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

    imageops colorize

    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

    ops colorize

    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

    ops colorize image
    python_pillow_function_reference.htm
    Advertisements