- Python Pillow - Home
- Python Pillow - Overview
- Python Pillow - Environment Setup
- Basic Image Operations
- Python Pillow - Working with Images
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Adding Borders to Images
- Python Pillow - Identifying Image Files
- Python Pillow - Merging Images
- Python Pillow - Cutting and Pasting Images
- Python Pillow - Rolling an Image
- Python Pillow - Writing text on image
- Python Pillow - ImageDraw Module
- Python Pillow - Concatenating two Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Creating a Watermark
- Python Pillow - Image Sequences
- Python Pillow Color Conversions
- Python Pillow - Colors on an Image
- Python Pillow - Creating Images With Colors
- Python Pillow - Converting Color String to RGB Color Values
- Python Pillow - Converting Color String to Grayscale Values
- Python Pillow - Change the Color by Changing the Pixel Values
- Image Manipulation
- Python Pillow - Reducing Noise
- Python Pillow - Changing Image Modes
- Python Pillow - Compositing Images
- Python Pillow - Working with Alpha Channels
- Python Pillow - Applying Perspective Transforms
- Image Filtering
- Python Pillow - Adding Filters to an Image
- Python Pillow - Convolution Filters
- Python Pillow - Blur an Image
- Python Pillow - Edge Detection
- Python Pillow - Embossing Images
- Python Pillow - Enhancing Edges
- Python Pillow - Unsharp Mask Filter
- Image Enhancement and Correction
- Python Pillow - Enhancing Contrast
- Python Pillow - Enhancing Sharpness
- Python Pillow - Enhancing Color
- Python Pillow - Correcting Color Balance
- Python Pillow - Removing Noise
- Image Analysis
- Python Pillow - Extracting Image Metadata
- Python Pillow - Identifying Colors
- Advanced Topics
- Python Pillow - Creating Animated GIFs
- Python Pillow - Batch Processing Images
- Python Pillow - Converting Image File Formats
- Python Pillow - Adding Padding to an Image
- Python Pillow - Color Inversion
- Python Pillow - M L with Numpy
- Python Pillow with Tkinter BitmapImage and PhotoImage objects
- Image Module
- Python Pillow - Image Blending
- Python Pillow Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - Function Reference
- Python Pillow - Useful Resources
- Python Pillow - Discussion
Python Pillow - Adding Filters to an Image
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