In this chapter, you will be learning how to apply filters on images by using Pillow ImageFilter module. Pillow ImageFilter module allows you to apply pre-defined filters to the images. These filters can be applied to the images by passing them to the filter()
method of the image.
You need to import the ImageFilter
module to make use of these filters.
from PIL import ImageFilter
Blur
ImageFilter.BLUR
filter can be used to make an image blurred.
Example
# Apply ImageFilter.BLUR to image and save it as img_blur
img_blur=img.filter(ImageFilter.BLUR)
img_blur.show()
Note– The blurring effect is also subject to the resolution of the image. Hence, an image with high resolution will appear to have a less blurring effect as compared to a low-resolution image. Therefore, try using this effect on a comparatively lower resolution image to notice its effects.
Contour
ImageFilter.CONTOUR
filter can be used to extract information about outlines in the image.
Example
# Apply ImageFilter.BLUR to image and save it as img_contour
img_contour=img.filter(ImageFilter.CONTOUR)
img_contour.show()
Find Edges
ImageFilter.FIND_EDGES
filter can be used to find the edges in the image.
Example
# Apply ImageFilter.FIND_EDGE to image and save it as img_find_edge
img_find_edge=img.filter(ImageFilter.FIND_EDGE)
img_find_edge.show()
Enhance Edge
ImageFilter.EDGE_ENHANCE
filter can be used to enhance the edges in the image.
Example
# Apply ImageFilter.EDGE_ENHANCE to image and save it as img_edge_enhance
img_edge_enhance=img.filter(ImageFilter.EDGE_ENHANCE)
img_edge_enhance.show()
ImageFilter.EDGE_ENHANCE_MORE
filter can be used to enhance the edges in the image even more.
Example
# Apply ImageFilter.EDGE_ENHANCE_MORE to image and save it as img_edge_enhance_more
img_edge_enhance_more=img.filter(ImageFilter.EDGE_ENHANCE_MORE)
img_edge_enhance_more.show()
Note– The edge enhance effect is also subject to the resolution of the image. Hence, an image with high resolution will appear to have a less edge enhancement effect as compared to a low-resolution image. Therefore, try using this effect on a comparatively lower resolution image to notice its effects.
Emboss
ImageFilter.EMBOSS
filter can be used to emboss the image. Embossing an image makes the image come ‘up’ to give a visual effect of high relief in the image.
Example
# Apply ImageFilter.EMBOSS to image and save it as img_emboss
img_emboss=img.filter(ImageFilter.EMBOSS)
img_emboss.show()
Detail
ImageFilter.DETAIL
filter can be used to enhance and hence improve the details of the image.
Example
# Apply ImageFilter.DETAIL to image and save it as img_detail
img_detail=img.filter(ImageFilter.DETAIL)
img_detail.show()
Sharpen
ImageFilter.SHARPEN
filter can be used to make the images sharper and hence make the image more clear.
Example
# Apply ImageFilter.SHARPEN to image and save it as img_sharpen
img_sharpen=img.filter(ImageFilter.SHARPEN)
img_sharpen.show()
Note– The sharpen effect is also subject to the resolution of the image. Hence, an image with high resolution will appear to have a less sharpening effect as compared to a low-resolution image. Therefore, try using this effect on a comparatively lower resolution image to notice its effects.
Smooth
ImageFilter.SMOOTH
filter can be used to reduce noise in the images and hence making them less pixelated.
Example
# Apply ImageFilter.SMOOTH to image and save it as img_smooth
img_smooth=img.filter(ImageFilter.SMOOTH)
img_smooth.show()
ImageFilter.EDGE_SMOOTH_MORE
filter can be used to enhance the smoothness of the image even more.
Example
# Apply ImageFilter.SMOOTH_MORE to image and save it as img_smooth_more
img_smooth_more=img.filter(ImageFilter.SMOOTH_MORE)
img_smooth_more.show()
Note– The smooth effect is also subject to the resolution of the image. Hence, an image with high resolution will appear to have a less smoothing effect as compared to a low-resolution image. Therefore, try using this effect on a comparatively lower resolution image to notice its effects.