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.
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.
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
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
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()
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
Following python example applies CONTOUR filter to the given image.
#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()
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 DETAIL filter to the given image.
#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()
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 EDGE_ENHANCE filter to the given image −
#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()
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 EDGE_ENHANCE_MORE filter to the given image.
#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()
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 EMBOSS filter to the given image.
#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()
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 FIND_EDGES filter to the given image.
#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()
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 SMOOTH filter to the given image.
#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()
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.
#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()
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.
#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()
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