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

    Selected Reading

    Python Pillow - ImageOps.solarize() Function



    The PIL.ImageOps.solarize function is used to invert pixel values above a specified threshold in a greyscale image.

    Syntax

    Following is the syntax of the function −

    PIL.ImageOps.solarize(image, threshold=128)
    

    Parameters

    Here are the details of this function parameters −

    • image − The image to solarize.

    • threshold − All pixels above this greyscale level are inverted. The default threshold is set to 128, which means that pixels with intensity values greater than 128 will be inverted.

    Return Value

    The function returns a new image object where pixel values above the specified threshold have been inverted.

    Examples

    Example 1

    In this example, the ImageOps.solarize function is used to invert pixel values above the default threshold of 128 in the input image.

    from PIL import Image, ImageOps
    
    # Open an image file
    input_image = Image.open("Images/butterfly.jpg")
    
    # Solarize the image with the default threshold
    solarized_image = ImageOps.solarize(input_image)
    
    # Display the original and solarized images
    input_image.show()
    solarized_image.show()
    

    Output

    Input Image

    butterfly on flower

    Output Image

    imageops solarize

    Example 2

    Here's another example using the PIL.ImageOps.solarize() function with a different image and different threshold.

    from PIL import Image, ImageOps
    
    # Open an image file
    input_image = Image.open("Images/Tajmahal_2.jpg")
    
    # Solarize the image with a specific threshold
    solarized_image = ImageOps.solarize(input_image, threshold=100)
    
    # Display the original and solarized images
    input_image.show()
    solarized_image.show()
    

    Output

    Input Image

    tajmahal birds

    Output Image

    ops solarize

    Example 3

    In this example, the code performs solarization with different threshold values of an image, and the results are displayed using Matplotlib.

    from PIL import Image, ImageOps
    import matplotlib.pyplot as plt
    
    # Open an image file
    input_image = Image.open("Images/flowers_1.jpg")
    
    # Define three different thresholds
    thresholds = [0, 50, 100]
    
    # Create subplots for original and solarized images
    num_thresholds = len(thresholds) + 1 
    fig, axes = plt.subplots(2, 2, figsize=(10, 8))
    ax = axes.ravel()
    
    # Display the original image
    ax[0].imshow(input_image)
    ax[0].set_title('Original Image')
    ax[0].axis('off')
    
    # Solarize the image with different thresholds and display the results
    for idx, threshold in enumerate(thresholds, start=1):
       solarized_image = ImageOps.solarize(input_image, threshold=threshold)
       
       # Display the solarized images
       ax[idx].imshow(solarized_image)
       ax[idx].set_title(f'Solarized (Threshold = {threshold})')
       ax[idx].axis('off')
    
    plt.tight_layout()
    plt.show()
    

    Output

    ops solarize image
    python_pillow_function_reference.htm
    Advertisements