Python Pillow adds filters to images
Adding Filters to Images with Python Pillow
The ImageFilter module contains definitions for a set of predefined filters, which we access using the Image.filter() method. These filters are used to change the look and feel of an image.
Example
The following example filters 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 the program above, we used the MinFilter() method, which is used to create a minimum filter. It selects the lowest pixel value in a window of a given size.
ImageFilter.MinFilter(size=3)
Where?
size – Kernel size in pixels.
Output
If you save the above program and execute it, it will display the original image, the blurred image, and the blurred image with the MinFilter using standard PNG display tools, as shown below.
Original Image
Blurred Image
Filters
The current version of the Pillow library provides a set of predefined image enhancement filters mentioned below.
- BLUR
- CONTOUR
- DETAIL
- EDGE_ENHANCE
- EDGE_ENHANCE_MORE
- EMBOSS
- FIND_EDGES
- SHARPEN
- SMOOTH
- SMOOTH_MORE
Example
The following Python example applies a blur filter to an image and displays it using standard PNG display tools.
#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
Similarly, you can pass any of the following parameters to the image.filter() method to obtain the corresponding output: â
- CONTOUR
- DETAIL
- EDGE_ENHANCE
- EDGE_ENHANCE_MORE
- EMBOSS
- FIND_EDGES
- SMOOTH
- SMOOTH_MORE
- SHARPEN
Python img.filter(CONTOUR) Method
The following Python example applies the CONTOUR filter to a 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, it will display the original image and the filtered image using standard PNG display tools, as shown below.
Original image
Filtered image
Python img.filter(DETAIL) Method
The following Python example applies the DETAIL filter to a 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, it will display the original image, as well as the filtered image using standard PNG display tools, as shown below.
Original image
Filtered image
Python img.filter(EDGE_ENHANCE) Method
The following Python example applies the 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, it will display the original image, as well as the filtered image using standard PNG display tools, as shown below.
Original image
Filtered image
Python img.filter(EDGE_ENHANCE_MORE) Method
The following Python example applies the EDGE_ENHANCE_MORE filter to a 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, it will display the original image, as well as the filtered image using standard PNG display tools, as shown below.
Original image
Filtered image
Python img.filter(EMBOSS) Method
The following Python example applies an EMBOSS filter to a 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, it will display the original image and the filtered image using standard PNG display tools, as shown below.
Original image
Filtered image
Python img.filter(FIND_EDGES) Method
The following Python example applies the FIND_EDGES filter to a 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, it will display the original image and the filtered image using standard PNG display tools, as shown below.
Original Image
Filtered Image
Python img.filter(SMOOTH) Method
The following Python example applies a SMOOTH filter to a 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, it will display the original image and the filtered image using standard PNG display tools, as shown below.
Original Image
Filtered Image
Python img.filter(SHARPEN) Method
The following Python example applies the SHARPEN filter to a 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, it will display the original image and the filtered image using standard PNG display tools, as shown below.
Original Image
Filtered Image
The following Python example applies the SHARPEN filter to a given image.
Example
#Import required image modules
from PIL import Image, ImageFilter
#Import all the enhancement filters 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, it will display the original image and the filtered image using standard PNG display tools, as shown below.
Original image
Filtered image