Open In App

Apply a Gauss filter to an image with Python

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A Gaussian Filter is a low-pass filter used for reducing noise (high-frequency components) and for blurring regions of an image. This filter uses an odd-sized, symmetric kernel that is convolved with the image. The kernel weights are highest at the center and decrease as you move towards the periphery, this makes the filter less sensitive to drastic changes (edges), allowing a smooth blur effect. The 2D Gaussian function is defined as:


where:

  • x, y are the coordinates
  • Mathematical Constant PI (value = 3.13)
  • σ is the Standard Deviation

Using this formula, you can calculate the Gaussian kernel of any size by providing appropriate values. Example: 3*3 gaussian kernal(σ =1)

Implementing gaussian blur in Python

Example 1: In this example, we will blur the entire image using Python's PIL (Pillow) library. The Gaussian blur is a commonly used effect in image processing to reduce image noise and detail.

Python
from PIL import Image, ImageFilter
img = Image.open(r"IMAGE_PATH")
blur = image.filter(ImageFilter.GaussianBlur())

blur.show()

Output

Blurred Image

Explanation:

  • Open the image file from "IMAGE_PATH" using Image.open().
  • Apply a default blur using .filter(ImageFilter.GaussianBlur()).
  • Display the blurred image using .show().

Example 2: Instead of blurring the entire image, sometimes we only want to blur a specific region (like faces or private information). This can be achieved by:

  • Cropping the region.
  • Applying Gaussian blur.
  • Pasting it back on the original image.
Python
from PIL import Image, ImageFilter

img = Image.open(r"IMAGE_PATH") # Load image
crop = img.crop((0, 0, 150, 150))  # Crop top-left region
blur = crop.filter(ImageFilter.GaussianBlur(3)) 
img.paste(blur, (0, 0))  
img.save("output.png")

Output

Only the top left region of the image blurred

Explanation:

  • Open the image from "IMAGE_PATH" using Image.open().
  • Extract the top-left 150x150 pixel region using .crop((0, 0, 150, 150)).
  • Apply Gaussian blur with radius 3 to the cropped region.
  • Replace the original region with the blurred one using .paste().

Next Article
Practice Tags :

Similar Reads

  翻译: