Sunday, January 14, 2024

Python Imaging Library (PIL): Image Processing and Image Enhancement

 PIL, also referred to as the Python Imaging Library, is an open-source library specifically created for editing pictures in the Python programming language. The program provides a wide range of image processing capabilities and is compatible with several image file formats. Since 2011, the original PIL project has stopped being actively maintained, and Pillow, a derivative of PIL, has replaced it as the updated version. Pillow is now undergoing vigorous development and is the favored choice for doing image processing tasks in the Python computer language.

ImageEnhance Module

The Python Imaging Library (PIL), also known as the Pillow library, offers the ImageEnhance module which consists of a collection of classes designed to enhance various aspects of images such as contrast, brightness, sharpness, and color. This module is beneficial for making basic modifications to the visual aesthetics of images. The ImageEnhance module works in conjunction with Pillow's Image objects.

Color

class PIL.ImageEnhance.Color(image)
Adjust the color balance of the image. This class is used for the purpose of adjusting the color balance of an image. Resembling the controls seen on a color television set. A zero enhancement factor results in a grayscale image. A factor of 1.0 results in the exact replication of the original image.

Code Example:
from PIL import Image
from PIL import ImageEnhance
# open image for color enhancement
image = Image.open("drd.jpg") # original image
color_enhance = ImageEnhance.Color(image)
images = []
for i in range(8):
    factor = i / 4.0
    im = color_enchance.enhance(factor)
    images.append(im)
import matplotlib.pyplot as plt     # display images in 2x4 grid
fig, axes = plt.subplots(2, 4, figsize = (12, 6))
for i, ax in enumerate(axes.flat):
    if i < len(images):
        ax.imshow(images[i])
        ax.axis('off')
plt.show()
Original Image

Image Color Enhancement

Contrast

class PIL.ImageEnhance.Contrast(image)
Modify the contrast of the picture. The purpose of this class is to manage the contrast of an image, similarly much to the contrast adjustment seen on a television screen. A factor of 0.0 produces a completely gray image, whereas a factor of 1.0 preserves the original image. Higher values amplify the contrast of the image.
Code Example:
from PIL import Image
from PIL import ImageEnhance
# open image for contrast enhancement
image = Image.open("drd.jpg") # original image
contrast_enhance = ImageEnhance.Contrast(image)
images = []
for i in range(8):
    factor = i / 4.0
    im = contrast_enchance.enhance(factor)
    images.append(im)
import matplotlib.pyplot as plt     # display images in 2x4 grid
fig, axes = plt.subplots(2, 4, figsize = (12, 6))
for i, ax in enumerate(axes.flat):
    if i < len(images):
        ax.imshow(images[i])
        ax.axis('off')
plt.show()
Original Image

Image Contrast Enhancement

Brightness

class PIL.ImageEnhance.Brightness(image)
Adjust the brightness of the image. The purpose of this class is to regulate the brightness of an image. A factor of 0.0 results in a completely dark picture, whereas a factor of 1.0 maintains the original image. Higher values of the factor increase the brightness of the image.
Code Example:
from PIL import Image
from PIL import ImageEnhance
# open image for brightness enhancement
image = Image.open("drd.jpg") # original image
brightness_enhance = ImageEnhance.Brightness(image)
images = []
for i in range(8):
    factor = i / 4.0
    im = brightness_enchance.enhance(factor)
    images.append(im)
import matplotlib.pyplot as plt     # display images in 2x4 grid
fig, axes = plt.subplots(2, 4, figsize = (12, 6))
for i, ax in enumerate(axes.flat):
    if i < len(images):
        ax.imshow(images[i])
        ax.axis('off')
plt.show()
Original Image

Image Brightness Enhancement

Sharpness

class PIL.ImageEnhance.Sharpness(image)
Adjust image sharpness. This class can be used to adjust the sharpness of an image. An enhancement factor of 0.0 gives a blurred image, a factor of 1.0 gives the original image, and a factor of 2.0 gives a sharpened image.
Code Example:
from PIL import Image
from PIL import ImageEnhance
# open image for sharpness enhancement
image = Image.open("drd.jpg") # original image
sharpness_enhance = ImageEnhance.Sharpness(image)
images = []
for i in range(8):
    factor = i / 4.0
    im = sharpness_enchance.enhance(factor)
    images.append(im)
import matplotlib.pyplot as plt     # display images in 2x4 grid
fig, axes = plt.subplots(2, 4, figsize = (12, 6))
for i, ax in enumerate(axes.flat):
    if i < len(images):
        ax.imshow(images[i])
        ax.axis('off')
plt.show()
Original Image

Image Sharpness Enhancement



No comments:

Post a Comment