Python Pillow - Enhancing Color



Enhancing color is an image processing technique that focuses on improving the visual appeal and quality of a digital image by fine-tuning the balance and saturation of its colors. The objective of image color enhancement is to get finer image details while highlighting the useful information. This technique is especially useful in scenarios where images have poor illumination conditions, resulting in darker and low-contrast appearances that require refinement. This technique is widely used in various fields, including graphic design, photography, and digital art, to enhance the visual impact of images.

The Python Pillow library (PIL) offers the Color() class within its ImageEnhance module, which enables you to apply color enhancement to images.

Enhancing Color of an Image

To enhance the color of an image using the Python Pillow library, you can use the ImageEnhance.Color() class, which allows you to adjust the color balance of an image, just as controls on a color TV set.

Following is the syntax of the ImageEnhance.Color() class −

class PIL.ImageEnhance.Color(image)

An enhancement factor represented as a floating-point value, is passed as an argument to the common single interface method, enhance(factor). This factor plays an important role in adjusting the color balance. A factor of 0.0 will give a black and white image. A factor of 1.0 gives the original image.

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

  • Create a color enhancer object using the ImageEnhance.Color() class.

  • Then apply the enhancement factor to the enhancer object using the enhance() method.

Example

Here is an example that enhances the color of an image with a factor of 3.0.

from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('Images/Tajmahal_2.jpg')

# Create a Color object
color_enhancer = ImageEnhance.Color(image)

# Display the original image
image.show()

# Enhance the color with a factor of 3.0
colorful_image = color_enhancer.enhance(3.0) 

# Display the enhanced color image
colorful_image.show()

Output

Input image −

tajmahal

Output highly color enhanced image −

highly color enhanced

Example

Here is an example that enhances the color of an image with a lower enhancement factor(0.0).

from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('Images/Tajmahal_2.jpg')

# Create a Color object
color_enhancer = ImageEnhance.Color(image)

# Display the original image
image.show()

# Enhance the color with a factor of 0.0
colorful_image = color_enhancer.enhance(0.0) 

# Display the enhanced color image
colorful_image.show()

Output

Input image −

tajmahal

Output color enhanced image with a factor of 0.0 −

imageenhance zero
Advertisements