Python Pillow – Enhancing Contrast


Python Pillow – Enhancing Contrast



”;


Enhancing contrast refers to the process of improving the visibility and quality of an image. It is an image processing technique that involves increasing the difference between the various elements within an image, such as objects, shapes, edges, and textures, by adjusting the distribution of intensity values or colors. This technique is widely used in fields such as medical imaging, computer vision, remote sensing, and photography to improve image quality and obtain more detailed visual information.

The Python Pillow (PIL) library offers the Contrast() class within its ImageEnhance module, for the application of contrast enhancement to images.

Enhancing Contrast of an Image

To adjust the contrast of an image, you can apply ImageEnhance.Contrast() class to an image object with the enhancment factor. It controls the contrast of an image, much like adjusting the contrast on a television set.

Below is the syntax of the ImageEnhance.Contrast() class −


class PIL.ImageEnhance.Contrast(image)

Following are the steps to achieve contrast enhancement of an image −

  • Create a contrast object using the ImageEnhance.Contrast() class.

  • Then apply the enhancement factor with the help of contrast_object.enhance() method.

An enhancement factor is a floating-point value passed to the common single interface method, enhance(factor), which plays an important role in adjusting image contrast. When a factor of 0.0 is used, it will produce a solid grey image. A factor of 1.0 will give the original image, and when greater values are used, the contrast of the image is increased, making it visually more distinct.

Example

The following example demonstrates how to achieve a highly contrasted image using the PIL.ImageEnhance module.


from PIL import Image, ImageEnhance

# Open the input image 
image = Image.open(''Images/flowers.jpg'')

# Create an ImageEnhance object for adjusting contrast
enhancer = ImageEnhance.Contrast(image)

# Display the original image
image.show()

# Enhance the contrast by a factor of 2 and display the result
enhancer.enhance(2).show()

Input Image


sky flowers

Output Image

Output a highly contrasted version of the input image −


image enhance contrast

Example

To reduce the contrast of an image, you can use a contrast enhancement factor less than 1. Here”s an example illustrating the creation of a low-contrast version of the input image using the PIL.ImageEnhance.Contrast class.


from PIL import Image, ImageEnhance

# Open the input image 
image = Image.open(''Images/flowers.jpg'')

# Create an ImageEnhance object for adjusting contrast
enhancer = ImageEnhance.Contrast(image)

# Display the original image
image.show()

# Reduce the contrast by a factor of 0.5 and display the result
enhancer.enhance(0.5).show()

Input Image


sky flowers

Output Image

Output a low contrasted version of the input image −


low contrasted image

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *