Python Pillow - ImageOps.equalize() Function



The PIL.ImageOps.equalize() function is used to equalize the histogram of an image, which means adjusting the intensity values of the pixels in the image so that the histogram becomes more uniform.

Syntax

Following is the syntax of the function −

PIL.ImageOps.equalize(image, mask=None)

Parameters

Here are the details of this function parameters −

  • image − The image to equalize. This is the input image on which the histogram equalization will be applied.

  • mask − An optional mask. If provided, only the pixels selected by the mask are included in the analysis. The mask is used to limit the equalization to specific regions of the image.

Return Value

An image − The function returns a new image object where the histogram has been equalized. The equalization process aims to create a uniform distribution of grayscale values in the output image.

Examples

Example 1

Here is an example of using the ImageOps.equalize() function to equalize the histogram of the input image.

from PIL import Image, ImageOps

# Open an image file
input_image = Image.open("Images/Car_2.jpg")

# Apply histogram equalization
equalized_image = ImageOps.equalize(input_image)

# Display the original and equalized images
input_image.show()
equalized_image.show()

Output

Input Image

balck and yellow car

Output Image

imageops equalize

Example 2

This example demonstrates how to use histogram equalization with a mask to selectively enhance the contrast of the input image.

from PIL import Image, ImageOps, ImageDraw

# Open an image file
input_image = Image.open("Images/Car_2.jpg")

# Define a mask
mask = Image.new("L", input_image.size, 0)
draw = ImageDraw.Draw(mask)
draw.ellipse((140, 50, 260, 170), fill=255)

# Apply histogram equalization with the mask
equalized_image = ImageOps.equalize(input_image, mask)

# Display the original and equalized images
input_image.show()
equalized_image.show()

Output

Input Image

balck and yellow car

Output Image

ops equalize

Example 3

Here's another example of using a rectangular mask to limit the histogram equalization of an input image.

from PIL import Image, ImageOps, ImageDraw

# Open an image file
input_image = Image.open("Images/Car_2.jpg")

# Define a mask (you can create a mask using various techniques)
mask = Image.new("L", input_image.size, 0)
draw = ImageDraw.Draw(mask)
draw.rectangle([(100, 100), (300, 300)], fill=255)

# Apply histogram equalization with the mask
equalized_image = ImageOps.equalize(input_image, mask=mask)

# Display the original, and equalized image 
input_image.show()
equalized_image.show()

Output

Input Image

balck and yellow car

Output Image

ops equalize image
python_pillow_function_reference.htm
Advertisements