Applying Gaussian filter on an image with Python
Applying a Gaussian Filter to an Image with Python
A Gaussian filter is a low-pass filter used to reduce noise (high-frequency components) and blur image areas. This filter is implemented as a symmetric kernel of odd size (a DIP version of a matrix) that is applied to every pixel in the region of interest to achieve the desired effect. This kernel has no difficulty with sharp changes in color (edges) because pixels at the center of the kernel are weighted more heavily than those at the periphery, resulting in the final value. A Gaussian filter can be considered an approximation of the Gaussian function (mathematically). In this article, we will learn how to use a Gaussian filter to reduce noise in an image using the Python programming language.
We will use the following image for demonstration.
Screenshot of a snippet from Windows Explorer
Process of Applying a Gaussian Filter
When applying a Gaussian filter to an image, we first define the size of the kernel/matrix used to filter the image. These dimensions are typically odd, meaning that the overall result can be calculated at the center pixel. Furthermore, the kernel is symmetric, so it has the same number of rows and columns. The values within the kernel are calculated using the Gaussian function, as shown below.
2D Gaussian function
Where:
x → X-coordinate value
y → Y-coordinate value
σ → Standard deviation
The above function can be used to calculate a Gaussian kernel of any size by providing it with appropriate values. The approximation (2D) of a 3×3 Gaussian kernel with a standard deviation of 1 is shown below.
Implementing a Gaussian kernel in Python
We will use PIL (Python Imaging Library) passes our entire image through a predefined Gaussian kernel. The help page for this function is as follows.
Syntax: Filter(Kernel)
Takes a kernel (predefined or custom) and passes each pixel of the image through it (kernel convolution).
Parameters: Filter Kernel
Returns: Image Object
In the following example, we will blur the image above.
# ImageFilter for using filter() function
from PIL import Image, ImageFilter
# Opening the image
# (R prefixed to string in order to deal with '' in paths)
image = Image.open(r"IMAGE_PATH")
# Blurring image by sending the ImageFilter.
# GaussianBlur predefined kernel argument
image = image.filter(ImageFilter.GaussianBlur)
# Displaying the image
image.show()
Output:
Blurred Image
Explanation:
First, we import the PIL library’s Image and ImageFilter modules (for using filter()). We then create an image object by opening an image in the user-defined path IMAGE_PATH . We then filter the image using the filter function, providing ImageFilter.GaussianBlur (predefined in the ImageFilter module) as an argument. The kernel size of ImageFilter.GaussianBlur is 5×5. Finally, we display the image.
Note: The kernel size can be manipulated by passing the kernel radius as an optional argument. This changes the following line.
image = image.filter(ImageFilter.GaussianBlur)
to
image = image.filter(ImageFilter.GaussianBlur(radius=x))
Where x is the blur radius (the kernel size in one direction, starting from the center pixel).
Blurring a Small Region in the Image
Instead of blurring the entire image, it’s possible to selectively blur certain parts of it. This can be done by first cropping the desired area from the image and then passing it through the filter() function. The output of this function (the blurred sub-image) is then pasted onto the original image. This will give us the desired output.
The code is as follows:
from PIL import Image, ImageFilter
image = Image.open(r"FILE_PATH")
# Cropping the image
smol_image = image.crop((0, 0, 150, 150))
# Blurring on the cropped image
blurred_image = smol_image.filter(ImageFilter.GaussianBlur)
# Pasting the blurred image on the original image
image.paste(blurred_image, (0,0))
# Displaying the image
image.save('output.png')
Output:
Only the upper left corner of the image is blurred