Image Enhancement in Python using Pillow

In our day-to-day lives, we encounter various instances when we need to enhance our image—Have you ever wondered if you can enhance your image with Python? Yes, you heard it right: Python allows us to do it with its built-in libraries. In this tutorial, we will learn how to enhance your image in Python using Pillow (Python Image Library PIL). It is used extensively in all the tasks related to image processing in Python. It is easy to use and work with, and we will be doing all the image-related tasks using Pillow.

Prerequisites

Before moving further, please ensure that Pillow is installed in your system using the code below in your command prompt or terminal.

pip show Pillow

The above command will show the details about the Pillow package if installed. If Pillow is not installed, install it by using the below code.

pip install Pillow

Step 1: Importing Libraries

Import all the libraries that you feel will be necessary.

import numpy as np
import matplotlib.pyplot as plt
from PIL import ImageEnhance, Image

Step 2: Load the image

Load the image from your system which you want to enhance. Use the below code, and remember to change the path. I will use my system’s image of a Lion for demonstration purposes.

image_path = 'data/lion.jpg'

img = Image.open(image_path)
img.show()

This will display the below image.

Load the image in pillow

Step 3: Enhancing the image

The ImageEnhance module consists of various classes that can be utilized for enhancing the image. Each class has a function enhance that takes the enhancement factor. If this factor is 1, the output will be the copy image of the input image. Greater than 1 implies you enhance the image by that much percentage, while less than 1 means you are doing the opposite.

np.hstack()is the function from Numpylibrary that horizontally stacks the input parameters. I am stacking the input image and output image of each class horizontally so that you can see the differences more clearly. To display the image, plt.imshow()function is used, which is a part of matplotlib library. I have kept the plot axis off so that axes don’t distract from viewing images. I am taking the enhancement factor equal to 2. You can take the factor as per your need.

Color enhancement

ImageEnhance.Color(img).enhance() is used to adjust the color balance of images just like you set in your smartphone and laptop.

colimg = ImageEnhance.Color(img).enhance(2)

output_img = np.hstack((img,colimg))

plt.figure(figsize=(50,50))
plt.axis('off')
plt.imshow(output_img)

Output:

Color enhance Image Enhancement in Python

Contrast enhancement

This is used to adjust the contrast of images like you set in your smartphone and laptop.

#Contrast

conimg = ImageEnhance.Contrast(img).enhance(2)

output_img = np.hstack((img,conimg))


plt.figure(figsize=(50,50))
plt.axis('off')
plt.imshow(output_img)

Output:

Contrast enhancement using Pillow in Python

Brightness enhancement

This is used to control the brightness of the Image.

#Brightness

brigimg = ImageEnhance.Brightness(img).enhance(2)

output_img = np.hstack((img,brigimg))

plt.figure(figsize=(50,50))
plt.axis('off')
plt.imshow(output_img)

Output:

Brightness enhancement in Python using Pillow

Sharpness enhancement

This is used to control the sharpness of the Image.

#Sharpness

sharpimg = ImageEnhance.Sharpness(img).enhance(2)

output_img = np.hstack((img,sharpimg))

plt.figure(figsize=(50,50))
plt.axis('off')
plt.imshow(output_img)

Output:

Sharpness enhancement in Python using Pillow

Leave a Reply

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