”;
Image filtering is a fundamental technique for modifying and enhancing images. In image processing, filters are mathematical operations applied to an image to improve its quality, extract specific information, or alter its appearance. They work at the pixel level, applying mathematical operations to pixels within a defined neighborhood, often determined by structuring elements or footprints. These filters are used for a wide range of tasks, including smoothing, sharpening, and enhancing specific image features. They can be implemented through techniques like convolution and frequency domain manipulation.
Python Pillow library provides the ImageFilter module with a predefined set of filters that can be applied to images using the Image.filter() method. These filters allow you to change the look and feel of images.
The current version of the Python Pillow library offers a range of predefined image enhancement filters. Some of the filters include −
- BLUR
- CONTOUR
- DETAIL
- EDGE_ENHANCE
- EDGE_ENHANCE_MORE
- EMBOSS
- FIND_EDGES
- SHARPEN
- SMOOTH
- SMOOTH_MORE
Applying Filter to an Image
By using the Image.filter() method you can apply a specific filter to an image object. This method takes a filter kernel as a parameter and returns an Image object with the applied filter effect.
Following is the syntax of the Image.filter() method −
Image.filter(filter)
This Image.filter() method accept only one parameter which is described below −
- filter − The filter kernel to be applied.
Example
Applying the BLUR Filter: Here”s an example of how to blur an input image using the ImageFilter.BLUR filter from the ImageFilter module.
from PIL import Image, ImageFilter # Open the input image image = Image.open(''Images/TP logo.jpg'') # Apply the BLUR filter to the image blurred_image = image.filter(filter=ImageFilter.BLUR) # Display the original and blurred images image.show() blurred_image.show()
Input Image
Output Image
Example
The following example demonstrates how to apply pillow predefined image enhancement filters to an input image.
from PIL import Image, ImageFilter import matplotlib.pyplot as plt # Open the input image image = Image.open(''Images/Flower1.jpg'') # Apply CONTOUR filter to the image image_contour = image.filter(filter=ImageFilter.CONTOUR) # Apply DETAIL filter to the image image_detail = image.filter(filter=ImageFilter.DETAIL) # Apply EDGE_ENHANCE filter to the image image_edge = image.filter(filter=ImageFilter.EDGE_ENHANCE) # Apply EDGE_ENHANCE_MORE filter to the image image_edge_more = image.filter(filter=ImageFilter.EDGE_ENHANCE_MORE) # Apply EMBOSS filter to the image image_emboss = image.filter(filter=ImageFilter.EMBOSS) # Apply FIND_EDGES filter to the image image_edges = image.filter(filter=ImageFilter.FIND_EDGES) # Apply SMOOTH filter to the image image_smooth = image.filter(filter=ImageFilter.SMOOTH) # Apply SMOOTH_MORE filter to the image image_smooth_more = image.filter(filter=ImageFilter.SMOOTH_MORE) # Apply SHARPEN filter to the image image_sharpen = image.filter(filter=ImageFilter.SHARPEN) # Create a subplot for each filtered image and display it using Matplotlib fig, ax = plt.subplots(3, 3, figsize=(12, 10)) # Original Image ax[0, 0].imshow(image) ax[0, 0].set_title("Original Image") # CONTOUR filter ax[0, 1].imshow(image_contour) ax[0, 1].set_title("CONTOUR") # DETAIL filter ax[0, 2].imshow(image_detail) ax[0, 2].set_title("DETAIL") # EDGE_ENHANCE filter ax[1, 0].imshow(image_edge) ax[1, 0].set_title("EDGE_ENHANCE") # EDGE_ENHANCE_MORE filter ax[1, 1].imshow(image_edge_more) ax[1, 1].set_title("EDGE_ENHANCE_MORE") # EMBOSS filter ax[1, 2].imshow(image_emboss) ax[1, 2].set_title("EMBOSS") # FIND_EDGES filter ax[2, 0].imshow(image_edges) ax[2, 0].set_title("FIND_EDGES") # SMOOTH filter ax[2, 1].imshow(image_smooth) ax[2, 1].set_title("SMOOTH") # SMOOTH_MORE filter ax[2, 2].imshow(image_smooth_more) ax[2, 2].set_title("SMOOTH_MORE") # Turn off ax for all subplots for a in ax.flatten(): a.axis(''off'') plt.tight_layout() plt.show()
Input Image
Output
”;