Blurring the Image
If a blurred image is observed carefully then a common thing to notice is that image is smooth meaning edges are not observed. A filter used for blurring is also called a low pass filter because it allows the low frequency to enter and stop high frequency. The ImageFilter class in the pillow library provides various filters that can be applied using the filter() method. Let’s see some of the blurring filters provided by the pillow.
Simple Blur
This method blurs the image using the kernel matrix or through the convolution matrix. It can be applied using the BLUR parameter.
Syntax:
filter(ImageFilter.BLUR)
Note: For more information refer, What is Image Blurring
Example:
# Importing Image class from PIL module
from PIL import Image, ImageFilter
# Opens a image in RGB mode
im = Image.open(r"geek.jpg")
# Blurring the image
im1 = im.filter(ImageFilter.BLUR)
# Shows the image in image viewer
im1.show()
Output:
Gaussian Blur
The Gaussian filter is implemented as an Odd sized Symmetric Kernel (DIP version of a Matrix) which is passed through each pixel of the Region of Interest to get the desired effect. The kernel is not hard towards drastic color changed (edges) due to the pixels towards the center of the kernel having more weightage towards the final value than the periphery. A Gaussian filter could be considered as an approximation of the Gaussian Function (mathematics). The Pillow module provides the predefined gaussianblur kernel that does the underlying maths for us.
Syntax:
ImageFilter.GaussianBlur(radius=2)
Example:
# Importing Image class from PIL module
from PIL import Image, ImageFilter
# Opens a image in RGB mode
im = Image.open(r"geek.jpg")
# Blurring the image
im1 = im.filter(ImageFilter.GaussianBlur(4))
# Shows the image in image viewer
im1.show()
Output:
Box blur
Box blur is also known as box linear filter. Box blurs are frequently used to approximate Gaussian blur. A box blur is generally implemented as an image effect that affects the whole screen. The blurred color of the current pixel is the average of the current pixel’s color and its 8 neighboring pixels. Pillow provides the BoxBlur() method to do the same.
Syntax:
ImageFilter.BoxBlur(radius)
Example:
# Importing Image class from PIL module
from PIL import Image, ImageFilter
# Opens a image in RGB mode
im = Image.open(r"geek.jpg")
# Blurring the image
im1 = im.filter(ImageFilter.BoxBlur(4))
# Shows the image in image viewer
im1.show()
Output:
Refer to the below articles to get detailed information about blurring images.
Python Pillow Tutorial
sinceDigital Image processing means processing the image digitally with the help of a computer. Using image processing we can perform operations like enhancing the image, blurring the image, extracting text from images, and many more operations. There are various ways to process images digitally. Here we will discuss the Pillow module of Python. Python Pillow is built on the top of PIL (Python Image Library) and is considered as the fork for the same as PIL has been discontinued since 2011. Pillow supports many image file formats including BMP, PNG, JPEG, and TIFF. The library encourages adding support for newer formats in the library by creating new file decoders.
This article aims at providing information about Python Pillow from basics to advance with the help of well-explained concepts and examples. So, let’s not waste any of the time and dive deep into the Pillow.