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 is2.
With different radius values, we get different blur intensities.
Example 1
from PIL import Image, ImageFilterimg = 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
ImageFilterandImagemodules. - Line 2: We loaded
Memcache.pnginto memory usingopen()inImagemodule. - Line 3: We apply the Gaussian filter with radius
50to the image we loaded in Line 2. - Line 5: We display the blurred image.