- Python Pillow Tutorial
- Python Pillow - Home
- Python Pillow - Overview
- Python Pillow - Environment Setup
- Python Pillow - Using Image Module
- Python Pillow - Working with Images
- Python Pillow - Creating Thumbnails
- Python Pillow - Merging Images
- Python Pillow - Blur an Image
- Python Pillow - Cropping an Image
- Python Pillow - Flip and Rotate Images
- Python Pillow - Resizing an Image
- Python Pillow - Creating a Watermark
- Python Pillow - Adding Filters to an Image
- Python Pillow - Colors on an Image
- Python Pillow - ImageDraw Module
- Python Pillow - Image Sequences
- Python Pillow - Writing Text on Image
- Python Pillow - M L with Numpy
- Python Pillow Useful Resources
- Python Pillow - Quick Guide
- Python Pillow - Useful Resources
- Python Pillow - Discussion
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 utility, as follows −
Original Image
Blurred Image
Image blurred with mini filter
Filters
The current version of pillow library 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 applies the blur filter on an image saves it and, displays it using standard PNG display utility −
#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 applies 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 utility, as follows −
Original image
Filtered image
Python img.filter(DETAIL) method
Following python example applies 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 utility, as follows −
Original image
Filtered image
Python img.filter(EDGE_ENHANCE) method
Following python example applies 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 utility, as follows −
Original image
Filtered image
Python img.filter(EDGE_ENHANCE_MORE) method
Following python example applies 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 utility, as follows −
Original image
Filtered image
Python img.filter(EMBOSS) method
Following python example applies 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 utility, as follows −
Original image
Filtered image
Python img.filter(FIND_EDGES) method
Following python example applies 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 utility, as follows −
Original image
Filtered image
Python img.filter(SMOOTH) method
Following python example applies 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 utility, as follows −
Original image
Filtered image
Python img.filter(SHARPEN) method
Following python example applies 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 utility, as follows −
Original image
Filtered image
Following python example applies 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 utility, as follows −
Original image
Filtered image
To Continue Learning Please Login
Login with Google