Image enhancement in PIL

Image Enhancement in PIL

Python Imaging Library (PIL) adds powerful image processing capabilities. It provides extensive file format support, efficient representation methods, and considerable image processing capabilities. The core imaging library is designed for fast access to data stored in a few basic pixel formats. It provides a solid foundation for general image processing tools.

Steps in Using PIL

Step 1: Import the imaging module from the PIL library.

from PIL import Image

This module provides a class of the same name that represents a PIL Image. It also provides various functions, including loading images from files and creating new images. I’m not going to explain the entire Image module here. However, here’s how you open an image file.

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

We’ll be using a PNG image here. One thing to remember—the image file you use here must exist in the same directory as your program. Otherwise, use the full path to the image file within quotes.

You can now see the image in your image browser with a single line of code.

image_variable_name.show()

Step 2: Now it’s time to import the most important module from the PIL library – the “ImageEnhance” module.

from PIL import ImageEnhance

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

Classes We Can Use for Enhancement

All enhancement classes implement a typical interface, containing a method called “enhance(factor)”.

PIL.ImageEnhance.[method](image_variable_name)

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

Parameters: The enhance() method takes a single parameter, factor, which is a floating point.
Return Type: This method returns an enhanced image.

The class details are as follows.

Brightness():

Adjusts the image brightness. This is used to control the brightness of the generated image. The code for brightness is as follows.

Input:

Image enhancement in PIL

from PIL import Image
from PIL import ImageEnhance
  
# Opens the image file
image = Image.open('gfg.png')
  
# shows image in image viewer
image.show()
  
  
#EnhanceBrightness
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 completely black image, while an enhancement factor of 1.0 produces the same image as the original.

Output:

Image Enhancement in PIL

Color():

Adjusts the image’s hue. This is used to control the hue of the generated image. The colorization code is as follows.

Input:

Image enhancement in PIL

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 completely black and white image, while an enhancement factor of 1.0 produces the same image as the original.

Output:

Image Enhancement in PIL

Contrast():

Adjusts the image contrast. This is used to control the contrast of the generated image. The code for changing the contrast is as follows.

Input:

Image enhancement in PIL

from PIL import Image
from PIL import ImageEnhance
  
# Opens the image file
image = Image.open('gfg.png')
  
# shows image in image viewer
image.show()
  
  
#EnhanceContrast
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 completely gray image, while an enhancement factor of 1.0 produces the same image as the original.

Output:

Image Enhancement in PIL

Sharpness():

Adjusts the sharpness of the image. This is used to control the sharpness of the generated image. The code for changing sharpness is as follows.

Input:

Image enhancement in PIL

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 the updated image in the image viewer
img_sharped.show()

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

Output:

Image Enhancement in PIL

These four classes are the most commonly used to enhance images. There are many other functions available. Don’t stop your learning here; explore more on your own.

Leave a Reply

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