Sunday, December 29, 2024
Google search engine
HomeLanguagesImage Enhancement in PIL

Image Enhancement in PIL

The Python Imaging Library(PIL) adds powerful image processing capabilities. It provides immense file format support, an efficient representation, and fairly powerful image processing capabilities. The core image library is intended for fast access to data stored in very few basic pixel formats. It provides a firm foundation for a general image processing tool.

Steps to use PIL

Step-1: Import the Image module from the PIL library.e

from PIL import Image

This module provides a class with an identical name which is employed to represent a PIL image. And also provides various functionalities, including functions to load images from files, and to create new images. I’m not gonna explain the whole Image module here. But, here’s how you’ll be able to open an image file.

image_variable_name = Image.open("lena.jpg")

We are going to use a PNG image here. One thing to remember- the image file you’re using here must be present within the same directory where your program is. Else use the full path of the image file within the quotation marks. 

Now you can see the image in your image viewer with a line of code.

image_variable_name.show()

Step-2: Now It is time to Import the foremost important module- ‘ImageEnhance’ module from the PIL library

from PIL import ImageEnhance

The ImageEnhance module contains various classes which will be used for image enhancement.

 

Classes We Can Use For Enhancement

All enhancement classes implement a typical interface, containing one method which is named the ‘enhance(factor)’ method.

PIL.ImageEnhance.[method](image_variable_name)

The method can be brightness, color, contrast, sharpness.

Parameter: The enhance() method takes just one parameter factor, i.e. a floating-point.
Return Type: This method returns an enhanced image. 

Classes are as follows:

Brightness(): 

Adjust image brightness. It’s accustomed to controlling the brightness of our resulting image. The code for brightness goes like below:

Input:

Python3




from PIL import Image
from PIL import ImageEnhance
  
# Opens the image file
image = Image.open('gfg.png')  
  
# shows image in image viewer
image.show()  
  
  
# Enhance Brightness
curr_bri = ImageEnhance.Brightness(image)
new_bri = 2.5
  
# Brightness enhanced by a factor of 2.5
img_brightened = curr_bri.enhance(new_bri)
  
# shows updated image in image viewer
img_brightened.show()  


An enhancement factor of 0.0 results in a full black image and an enhancement factor of 1.0 results the same as the original image.

Output:

Color():

Adjust image color level. It’s accustomed to controlling the color level of our resulting image. The code for coloring goes like below:

Input:

Python3




from PIL import Image
from PIL import ImageEnhance
  
# Opens the image file
image = Image.open('gfg.png')
  
# shows image in image viewer
image.show()
  
  
# Enhance Color Level
curr_col = ImageEnhance.Color(image)
new_col = 2.5
  
# Color level enhanced by a factor of 2.5
img_colored = curr_col.enhance(new_col)
  
# shows updated image in image viewer
img_colored.show()


An enhancement factor of 0.0 results in a full black and white image and an enhancement factor of 1.0 results the same as the original image.

Output:

Contrast():

Adjust image Contrast. It is accustomed to controlling the contrast of our resulting image. The code for changing contrast goes like below:

Input:

Python3




from PIL import Image
from PIL import ImageEnhance
  
# Opens the image file
image = Image.open('gfg.png')
  
# shows image in image viewer
image.show()
  
  
# Enhance Contrast
curr_con = ImageEnhance.Contrast(image)
new_con = 0.3
  
# Contrast enhanced by a factor of 0.3
img_contrasted = curr_con.enhance(new_con)
  
# shows updated image in image viewer
img_contrasted.show()  


An enhancement factor of 0.0 results in a full grey image and an enhancement factor of 1.0 results the same as the original image.

Output:

Sharpness():

Adjust image Sharpness. It’s accustomed to controlling the sharpness of our resulting image. The code for changing sharpness goes like below:

Input:

Python3




from PIL import Image
from PIL import ImageEnhance
  
# Opens the image file
image = Image.open('gfg.png')
  
# shows image in image viewer
image.show()
  
# Enhance Sharpness
curr_sharp = ImageEnhance.Sharpness(image)
new_sharp = 8.3
  
# Sharpness enhanced by a factor of 8.3
img_sharped = curr_sharp.enhance(new_sharp)
  
# shows updated image in image viewer
img_sharped.show()


An enhancement factor of 0.0 results in a blurred image and an enhancement factor of 1.0 results in the same as the original image and a factor > 1.0 results in a sharpened image.

Output:

These 4 classes are the most used, to enhance an image. A lot of other functionalities available there. Don’t stop your learning here, go and explore more by yourself.

RELATED ARTICLES

Most Popular

Recent Comments