Sharpen-filter Using Pillow - The Python Image Processing Library

Overview:

  • A sharpening filter makes the transition between the various regions present in an image more obvious rather than being smooth.
  • As an image passes through a sharpening filter the brighter pixels are boosted as relative to its neighbors. 

Sharpening an image using Python Image processing Library – Pillow:

  • The class ImageFilter.SHARPEN of the Pillow library implements a spatial filter using convolution to sharpen a given image.
  • An image object is constructed by passing a file name of the Image to the open() method of the Pillow’s Image class.
  • To get a filter applied onto an image the filter() method is called on the Image object. Only the class name of the filter is passed as the parameter.
  • In the Python example below, the name of the filter class passed is ImageFilter.SHARPEN, an object of which is created within. ImageFilter.SHARPEN has the convolution matrix for sharpening.
  • The convolution matrix used is,

(-2, -2, -2,

-2, 32, -2,

-2, -2, -2)

a 3x3 matrix.

  • The filter() method applies the convolution matrix to the image pixels and returns the sharpened image.

Example:

from PIL import Image
from PIL import ImageFilter

# Open an already existing image
imageObject = Image.open("./MountainsAndOcean.jpg");
imageObject.show();

# Apply sharp filter
sharpened1 = imageObject.filter(ImageFilter.SHARPEN);
sharpened2 = sharpened1.filter(ImageFilter.SHARPEN);

# Show the sharpened images
sharpened1.show();
sharpened2.show();

Output:

Original Image:

Before applying sharpening filter using Pillow, The Python Image Processing Library

Image after applying the Pillow's ImageFilter.SHARPEN filter once:

Sharpen filter applied once on the original image using Pillow-the Python Image Processing Library

Image after applying the Pillow's ImageFilter.SHARPEN filter twice:

 

The sharpened filter applied twice using pillow


Copyright 2020 © pythontic.com