Python Pillow - Enhancing Edges



Enhancing edges is a commonly used technique in image processing and computer vision to improve the interpretation and analysis of images. It refers to the process of emphasizing or highlighting the boundaries between different objects or regions in an image. And it aims to improve the visibility of edges, making them more distinct and prominent.

Python Pillow library provides the ImageFilter module with a predefined set of filters that can be applied to images using the Image.filter() method. The current version of the library offers two predefined image enhancement filters namely EDGE_ENHANCE and EDGE_ENHANCE_MORE to enhance edges in an image.

Enhancing Edges using the ImageFilter.EDGE_ENHANCE Kernel

This ImageFilter.EDGE_ENHANCE kernel filter is designed to Enhance the edges in the image.

Following are the steps for enhancing the edges in the image −

  • Load the input image using the Image.open() function.

  • Apply the filter() function to the loaded Image object and Provide ImageFilter.EDGE_ENHANCE as an argument to the Image.filter() function, then it returns a PIL.Image.Image object with enhanced edges.

Example

Here's an example that demonstrates how to use the ImageFilter.EDGE_ENHANCE filter kernel to enhance edges in an image.

from PIL import Image, ImageFilter

# Open the input image
image = Image.open('Images/Flower1.jpg')

# Apply edge enhancement filter
enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE)

# Display the original and enhanced images
image.show()
enhanced_image.show()

Input Image

pink_flower.jpg

Output Image

Output image with enhanced edges −

imagefilter edge enhance

Enhancing Edges using the EDGE_ENHANCE_MORE Kernel

This approach works similar to the EDGE_ENHANCE filter, but it applies a stronger enhancement to edges.

Example

Here's an example that demonstrates how to use the EDGE_ENHANCE_MORE filter kernel to enhance edges in an image more aggressively.

from PIL import Image, ImageFilter

# Open the input image
image = Image.open('Images/Flower1.jpg')

# Apply the EDGE_ENHANCE_MORE filter to the image
enhanced_image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)

# Display the original and enhanced images
image.show()
enhanced_image.show()

Input Image

pink_flower.jpg

Output Image

Output image after applying the EDGE_ENHANCE_MORE filter to the input image −

edge enhance more
Advertisements