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.posterize() Function



    The PIL.ImageOps.posterize function is used to reduce the number of bits for each color channel in an image, effectively limiting the color palette.

    Syntax

    Following is the syntax of the function −

    PIL.ImageOps.posterize(image, bits)
    

    Parameters

    Here are the details of this function parameters −

    • image − The image to posterize.

    • bits − The number of bits to keep for each channel (1-8). The value of bits determines the number of color levels for each channel, and it ranges from 1 to 8.

    Return Value

    The function returns a new image object where the input image has been posterized with the specified number of bits.

    Examples

    Example 1

    Heres an example that demonstrates how to create a new image with just 4-bits per channel.

    from PIL import Image, ImageOps
    
    # Open an image file
    input_image = Image.open("Images/flowers.jpg")
    
    # Posterize the image with 4 bits per channel
    posterized_image = ImageOps.posterize(input_image, bits=4)
    
    # Display the original and posterized images
    input_image.show()
    posterized_image.show()
    

    Output

    Input Image

    sun rays on pink flower

    Output Image

    imageops posterize

    Example 2

    Here's another example using the PIL.ImageOps.posterize function with a different image.

    from PIL import Image, ImageOps
    
    # Open an image file
    input_image = Image.open("Images/Tajmahal_2.jpg")
    
    # Posterize the image with 2 bits per channel
    posterized_image = ImageOps.posterize(input_image, bits=2)
    
    # Display the original and posterized images
    input_image.show()
    posterized_image.show()
    

    Output

    Input Image

    tajmahal and birds

    Output Image

    ops posterize

    Example 3

    In this example, the code performs posterization with different bit values to the image. And we use the Matplotlib to display the results.

    from PIL import Image, ImageOps
    import matplotlib.pyplot as plt
    
    # Open the original image
    original_image = Image.open('Images/flowers_1.jpg')
    
    # Create subplots for original and posterized images
    num_bits_list = [1, 2, 3, 4, 5, 6, 7, 8]
    
    fig, axes = plt.subplots(3, 3, figsize=(10, 15))
    
    # Display the original image
    axes[0, 0].imshow(original_image)
    axes[0, 0].set_title('Original Image')
    axes[0, 0].axis('off')
    
    # Perform posterization with different bit values and display the images
    for idx, num_bits in enumerate(num_bits_list, start=1):
       posterized_image = ImageOps.posterize(original_image, bits=num_bits)
       
       # Display the posterized images
       axes[idx // 3, idx % 3].imshow(posterized_image)
       axes[idx // 3, idx % 3].set_title(f'Posterized {num_bits} bits')
       axes[idx // 3, idx % 3].axis('off')
    
    plt.tight_layout()
    plt.show()
    

    Output

    ops posterize image
    python_pillow_function_reference.htm
    Advertisements