- Python Pillow - Discussion
- Python Pillow - Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - M L with Numpy
- Python Pillow - Writing Text on Image
- Python Pillow - Image Sequences
- Python Pillow - ImageDraw Module
- Python Pillow - Colors on an Image
- Python Pillow - Adding Filters to an Image
- Python Pillow - Creating a Watermark
- Python Pillow - Resizing an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Cropping an Image
- Python Pillow - Blur an Image
- Python Pillow - Merging Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Working with Images
- Python Pillow - Using Image Module
- Python Pillow - Environment Setup
- Python Pillow - Overview
- Python Pillow - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python Pillow - Adding Filters to an Image
The ImageFilter module contains definitions for a pre-defined set of filters, which we used with Image.filter() method. These filters are used to change the looks and feel of the image.
Example
Below example is Filtering an image −
from PIL import Image, ImageFilter im = Image.open( jungleSaf2.jpg ) im1 = im.filter(ImageFilter.BLUR) im1.show() im2 = im.filter(ImageFilter.MinFilter(3)) im2.show() im3 = im.filter(ImageFilter.MinFilter) # same as MinFilter(3) im3.show()
In above program, we have used the MinFilter() method, which is used to create a minimum filter. It picks the lowest pixel value in a window with the given size.
ImageFilter.MinFilter(size=3)
Where,
size − The kernel size, in pixels.
Output
If you save the above program and execute, it shows the original image, blurred image and, the blurred image with MinFilter using standard PNG display utipty, as follows −
Original Image
Blurred Image
Image blurred with mini filter
Filters
The current version of pillow pbrary provides below mentioned set of predefined image enhancement filters.
BLUR
CONTOUR
DETAIL
EDGE_ENHANCE
EDGE_ENHANCE_MORE
EMBOSS
FIND_EDGES
SHARPEN
SMOOTH
SMOOTH_MORE
Example
Following python example apppes the blur filter on an image saves it and, displays it using standard PNG display utipty −
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(BLUR) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
In the same way, to the image.filter() method you can pass any of the following parameters to get respective outputs −
CONTOUR
DETAIL
EDGE_ENHANCE
EDGE_ENHANCE_MORE
EMBOSS
FIND_EDGES
SMOOTH
SMOOTH_MORE
SHARPEN
Python img.filter(CONTOUR) method
Following python example apppes CONTOUR filter to the given image.
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(CONTOUR) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Python img.filter(DETAIL) method
Following python example apppes DETAIL filter to the given image.
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(DETAIL) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image, and the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Python img.filter(EDGE_ENHANCE) method
Following python example apppes EDGE_ENHANCE filter to the given image −
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(EDGE_ENHANCE) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image, and the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Python img.filter(EDGE_ENHANCE_MORE) method
Following python example apppes EDGE_ENHANCE_MORE filter to the given image.
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(EDGE_ENHANCE_MORE) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image, and the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Python img.filter(EMBOSS) method
Following python example apppes EMBOSS filter to the given image.
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(EMBOSS) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Python img.filter(FIND_EDGES) method
Following python example apppes FIND_EDGES filter to the given image.
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(FIND_EDGES) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Python img.filter(SMOOTH) method
Following python example apppes SMOOTH filter to the given image.
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(SMOOTH) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Python img.filter(SHARPEN) method
Following python example apppes SHARPEN filter to the given image.
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(SHARPEN) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Following python example apppes SHARPEN filter to the given image.
Example
#Import required image modules from PIL import Image, ImageFilter #Import all the enhancement filter from pillow from PIL.ImageFilter import ( BLUR, CONTOUR, DETAIL, EDGE_ENHANCE, EDGE_ENHANCE_MORE, EMBOSS, FIND_EDGES, SMOOTH, SMOOTH_MORE, SHARPEN ) #Create image object img = Image.open( images/cat.jpg ) #Applying the blur filter img1 = img.filter(SHARPEN) img1.save( images/ImageFilter_blur.jpg ) img1.show()
Output
If you save the above program and execute, it shows the original image and, the filtered image using standard PNG display utipty, as follows −
Original image
Filtered image
Advertisements