Open In App

Apply a Gauss filter to an image with Python

Last Updated : 26 Dec, 2020
Like Article

A Gaussian Filter is a low pass filter used for reducing noise (high frequency components) and blurring regions of an image. The 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 it the pixels towards the center of the kernel having more weightage towards the final value then the periphery. A Gaussian Filter could be considered as an approximation of the Gaussian Function (mathematics). In this article we will learn methods of utilizing Gaussian Filter to reduce noise in images using Python programming language.

We would be using the following image for demonstration:

A screenshot of a segment of windows explorer

Process to Apply a Gauss filter

In the process of using Gaussian Filter on an image we firstly define the size of the Kernel/Matrix that would be used for demising the image. The sizes are generally odd numbers, i.e. the overall results can be computed on the central pixel. Also the Kernels are symmetric & therefore have the same number of rows and column. The values inside the kernel are computed by the Gaussian function, which is as follows:

2 Dimensional gaussian function


x → X coordinate value

y → Y coordinate value

????  → Mathematical Constant PI (value = 3.13)

σ → Standard Deviation

Using the above function a gaussian kernel of any size can be calculated, by providing it with appropriate values. A 3×3 Gaussian Kernel Approximation(two-dimensional) with Standard Deviation = 1, appears as follows

Implementing the Gaussian kernel in Python

We would be using PIL (Python Imaging Library) function named filter() to pass our whole image through a predefined Gaussian kernel. The function help page is as follows:

Syntax: Filter(Kernel) 

Takes in a kernel (predefined or custom) and each pixel of the image through it (Kernel Convolution). 

Parameter: Filter Kernel

Return: Image Object

In the following example, we would be blurring the aforementioned image. 


# 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_PATH")
# Blurring image by sending the ImageFilter.
# GaussianBlur predefined kernel argument
image = image.filter(ImageFilter.GaussianBlur)
# Displaying the image


Blurred Image


Firstly we imported the Image and ImageFilter (for using filter()) modules of the PIL library. Then we created an image object by opening the image at the path IMAGE_PATH (User defined). After which we filtered the image through the filter function, and providing ImageFilter.GaussianBlur (predefined in the ImageFilter module) as an argument to it. The kernel dimensions of ImageFilter.GaussianBlur is 5×5. In the end we displayed the image. 

Note: The size of kernel could be manipulated by passing as parameter (optional) the radius of the kernel. This changes the following line from.

image = image.filter(ImageFilter.GaussianBlur)


image = image.filter(ImageFilter.GaussianBlur(radius=x))

where x => blur radius (size of kernel in one direction, from the center pixel)

Blurring a small region in an image:

Instead of the whole image, certain sections of it could also be selectively blurred. This could be performed by firstly cropping the desired region of the image, and then passing it through the filter() function. The output of which (the blurred sub image) would be pasted on top of the original image. This would give us the desired output. 

The code for which is as follows:


from PIL import Image, ImageFilter
image ="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'output.png')


Only the top left region of the image blurred

Previous Article
Next Article

Similar Reads

Spatial Filters - Averaging filter and Median filter in Image Processing
Spatial Filtering technique is used directly on pixels of an image. Mask is usually considered to be added in size so that it has a specific center pixel. This mask is moved on the image such that the center of the mask traverses all image pixels.In this article, we are going to cover the following topics - To write a program in Python to implement
3 min read
random.gauss() function in Python
random module is used to generate random numbers in Python. Not actually random, rather this is used to generate pseudo-random numbers. That implies that these randomly generated numbers can be determined. random.gauss() gauss() is an inbuilt method of the random module. It is used to return a random floating point number with gaussian distribution
2 min read
Gauss's Forward Interpolation
Interpolation refers to the process of creating new data points given within the given set of data. The below code computes the desired data point within the given range of discrete data sets using the formula given by Gauss and this method is known as Gauss's Forward Method. Gauss's Forward Method: The gaussian interpolation comes under the Centra
3 min read
Create a gauss pulse using scipy.signal.gausspulse
Prerequisites: Scipy The impulse response of a Gaussian Filter is written as a Gaussian Function as follows: [Tex]g(t) = \frac{1}{\sqrt{2 \pi } \sigma} e^{- \frac{t^2}{2 \sigma^2}}[/Tex] Its result is also Gaussian. In this article, we will plot the gauss pulse at 3Hz using scipy and matplotlib Python library. Gauss pulse is used in digital filters
2 min read
Difference between Low pass filter and High pass filter
IntrWhen it comes to processing signals, filtering is a key aspect that helps in shaping the characteristics of the signal. Low-pass and high-pass filters are two commonly used types of filters that work in opposite ways to filter signals. Low-pass filters, as the name suggests, allow low-frequency signals to pass through while attenuating high-fre
3 min read
Python PIL | Image filter with ImageFilter module
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The ImageFilter module contains definitions for a pre-defined set of filters, which can be used with the Image.filter() method. Image used: Filters - The current version of the library provides the set of predefined image enhancement filters: 1.
2 min read
Image Processing in Java - Colored Image to Grayscale Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and set PixelsIn this article, we will be converting a colored image to a grayscale image. RGB Color Model - The RGB color model is an additive mixing model in which red, green, and blue light are added together in various ways to reproduce a broad array of color
3 min read
Image Processing in Java - Colored image to Negative Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored image to Grayscale Image Conversion In this set, we will be converting a colored image to a negative image. Colored Image (RGB Color Model) - The RGB color model is an additive mixing model in which red, green, and
3 min read
Image Processing in Java - Colored Image to Sepia Image Conversion
Prerequisites: Image Processing in Java - Read and WriteImage Processing In Java - Get and Set PixelsImage Processing in Java - Colored Image to Grayscale Image ConversionImage Processing in Java - Colored Image to Negative Image ConversionImage Processing in Java - Colored to Red Green Blue Image Conversion In this set, we will be converting a col
3 min read
MATLAB - Ideal Lowpass Filter in Image Processing
In the field of Image Processing, Ideal Lowpass Filter (ILPF) is used for image smoothing in the frequency domain. It removes high-frequency noise from a digital image and preserves low-frequency components. It can be specified by the function- [Tex] $H(u, v)=\left\{\begin{array}{ll}1 & D(u, v) \leq D_{0} \\ 0 & D(u, v)>D_{0}\end{array}\
3 min read
Practice Tags :