Applying Edge Enhancement Filters Using Pillow

Overview:

  • Pillow - the Python Image Processing Library, provides several filters that can be applied on an Image Object including the Edge Enhancement Filters.

 

  • An Edge Enhancement Filter works by increasing the contrast of the pixels around the specific edges, so that the edges are visible prominently after applying the filter.

 

  • The edge filter can be selected through the Pillow provided constants ImageFilter.EDGE_ENHANCE and ImageFilter.EDGE_ENHANCE_MORE.

 

  • By calling the convert() method on an Image object with ImageFilter.EDGE_ENHANCE or ImageFilter.EDGE_ENHANCE_MORE as parameter, one of the edge filters can be applied.

 

  • The convolution matrices used by pillow for the image filters ImageFilter.EDGE_ENHANCE and ImageFilter.EDGE_ENHANCE_MORE which are also called as kernels or masks are provided here:
  • Kernel for ImageFilter.EDGE_ENHANCE:

            (

                -1, -1, -1,

                -1, 10, -1,

                -1, -1, -1

            )       

       

  •         Kernel for ImageFilter.EDGE_ENHANCE_MORE:

            (

                -1, -1, -1,

                -1,  9, -1,

                -1, -1, -1

            ) 

 

  • Both the kernels used by Pillow for edge enhancement filters are of dimensions 3x3.

 

Example:

# import image module

from PIL import Image

from PIL import ImageFilter

 

# Open an already existing image

imageObject = Image.open("./sand.jpg")

 

# Apply edge enhancement filter

edgeEnahnced = imageObject.filter(ImageFilter.EDGE_ENHANCE)

 

# Apply increased edge enhancement filter

moreEdgeEnahnced = imageObject.filter(ImageFilter.EDGE_ENHANCE_MORE)

 

# Show original image - before applying edge enhancement filters

imageObject.show() 

 

# Show image - after applying edge enhancement filter

edgeEnahnced.show()

 

# Show image - after applying increased edge enhancement filter

moreEdgeEnahnced.show()

Output:

Before applying edge enhancement filter to the image - Pillow Example:

 

After applying edge enhancement filter to the image:

After applying edge enhancement filter to the image - Pillow Example

After applying increased edge enhancement filter to the image:

After applying increased edge enhancement filter to the image - Pillow Example

 


Copyright 2018 © pythontic.com