Python Pillow blurs an image

Blurring an Image with Python Pillow

Blurring an image can be achieved by applying a filter to it to reduce the noise level in the image. Image blurring is an important aspect of image processing.

The ImageFilter class in the Pillow library provides several standard image filters. Image filters can be applied to an image by calling the filter() method of the Image object with the desired filter type defined in the ImageFilter class.

There are various techniques for blurring an image, and we will discuss the techniques mentioned below.

  • Simple Blur
  • Box Blur

  • Gaussian Blur

All three techniques use the “Image.filter()” method to filter the image.

Simple Blur

It applies a blurring effect to an image using a specific kernel or convolution matrix.

Syntax

filter(ImageFilter.BLUR)

Example

#Import required Image library
from PIL import Image, ImageFilter

#Open existing image
OriImage = Image.open('images/boy.jpg')
OriImage.show()

blurImage = OriImage.filter(ImageFilter.BLUR)
blurImage.show()
#Save blurImage
blurImage.save('images/simBlurImage.jpg')

When executed, the above example creates two standard PNG display tool windows (in this case, the Windows Photos application).

Original image

Python Pillow - Blur an image

Blurred image

Python Pillow - Blur an image

Box Blur

In this filter, we use the “radius” parameter. The radius is proportional to the blur value.

Syntax

ImageFilter.BoxBlur(radius)

Where:

  • Radius- The size of the box in one direction.
  • Radius 0– means no blur and returns the same image.

  • RRadius 1 &minnus takes 1 pixel in each direction, for a total of 9 pixels.

Example

#Import required Image library
from PIL import Image,

#Open existing image
OriImage = Image.open('images/boy.jpg')
OriImage.show()

#Applying BoxBlur filter
boxImage = OriImage.filter(ImageFilter.BoxBlur(5))
boxImage.show()

#Save Boxblur image
boxImage.save('images/boxblur.jpg')

Output

When executed, the above example creates two standard PNG display tool windows (in this case, the Windows Photos application).

Original image

Python Pillow - Blur an image

Blurred image

Python Pillow - Blur an image

Gaussian Blur

This filter also uses a parameterized radius and does the same thing as the box blur, but with a slightly different algorithm. In short, changing the radius will produce a Gaussian blur of varying strength.

Syntax

ImageFilter.GaussianBlur(radius=2)

Where.

  • radius – Blur radius

Example

#Import required Image library
from PIL import Image, ImageFilter

#Open existing image
OriImage = Image.open('images/boy.jpg')
OriImage.show()

#Applying GaussianBlur filter
gaussImage = OriImage.filter(ImageFilter.GaussianBlur(5))
gaussImage.show()

#Save Gaussian Blur Image
gaussImage.save('images/gaussian_blur.jpg')

Output

When executed, the above example creates two standard PNG display tool windows (in this case, the Windows Photos application).

Original image

Python Pillow - Blur an image

Blurred image

Python Pillow - Blur an image

Leave a Reply

Your email address will not be published. Required fields are marked *