How to use the Gaussian blur filter from PIL to blur an image

The ImageFilter module

The ImageFilter module in PIL contains definitions for a pre-defined set of filters that are used for different purposes.

The GaussianBlur function

The Gaussian filter is a type of low-pass filter that reduces high-frequency components. Instead of using the box filter, the image is convolved using a Gaussian filter.

Syntax

PIL.ImageFilter.GaussianBlur(radius=2)

Parameter

  • radius: This is the blur radius or the standard deviation of the Gaussian kernel. The default value is 2.

With different radius values, we get different blur intensities.

Example 1

from PIL import Image, ImageFilter
img = Image.open("/code/Memcache.png")
img1 = img.filter(ImageFilter.GaussianBlur(radius=50))
img1.save('output/graph.png') #Used internally on our platform for calling show(). Ignore if you are implementing locally.
img1.show()

Explanation

  • Line 1: We import the ImageFilter and Image modules.
  • Line 2: We loaded Memcache.png into memory using open() in Image module.
  • Line 3: We apply the Gaussian filter with radius 50 to the image we loaded in Line 2.
  • Line 5: We display the blurred image.

Free Resources