How to blur an image in Python pillow

Pillow is a fork for the Python Imaging Library, PIL, that provides various image-processing features to manipulate images and apply different filters. It is a powerful library that is easy to integrate with other Python libraries and frameworks. By using the pillow library, we can perform various operations on an image as follows:

In this Answer, we shall apply different types of blur filter effects on an image and observe how the image changes.

Required imports

We will use the PIL library to manipulate the image and then use matplotlib to display the results once the code is executed successfully.

import matplotlib.pyplot as plt
from PIL import Image, ImageFilter
import requests
from io import BytesIO
  • matplotlib: To create visualizations and plots in the Python library.

  • PIL: Computer vision library to apply operations on images.

    • Image: To manipulate the images.

    • ImageFilter: To access different filters, including blur.

  • requests: To send the requests over the HTTP server to the website.

  • BytesIO: To handle the binary data as an in-memory system.

Apply simple blur

A simple blur is an image-blurring technique that replaces the image pixel values with the mean value of the neighboring pixel. This reduces the image's noise and decreases the edges' sharpness, resulting in a comparatively smoother appearance.

Expected output

When a simple blur is applied to the original picture, its overall resolution decreases as all the pixels are changed to the average pixel value, and the appearance is more smooth.

Apply simple blur on an image.
Apply simple blur on an image.

Example code

In this example, we fetch an online image through its link address and apply a simple blur effect by using ImageFilter.BLUR.

import matplotlib.pyplot as plt
from PIL import Image, ImageFilter
import requests
from io import BytesIO

image_url = "https://images.pexels.com/photos/699466/pexels-photo-699466.jpeg?auto=compress&cs=tinysrgb&w=300"

#fetch image
response = requests.get(image_url)
image_data = BytesIO(response.content)
original_image = Image.open(image_data)

#Apply simple blur
blur_image = original_image.filter(ImageFilter.BLUR)

plt.figure(figsize=(10, 5))
plt.subplots_adjust(wspace=0.4, hspace=0.1)


plt.subplot(1, 2, 1)
plt.imshow(original_image)
plt.axis('off')
plt.title('Original Image')

plt.subplot(1, 2, 2) 
plt.imshow(blur_image)
plt.axis('off')
plt.title('Blurred Image')

plt.show()
SImple blur image.

Code explanation

  • Lines 1–4: Import the required libraries and modules.

  • Line 6: Store the link of the image that is to be used on the image_url. We can also use a local image file and give its exact name.

  • Lines 9–11: Use request to fetch the image from the server and open it using the Image.open() method and pass the converted image to it.

  • Line 14: Use the filter method of Image class to apply the BLUR() effect.

  • Lines 16–17: Specify the figure size that is to appear in the plot and assign the grid size.

  • Lines 20–23: Display the image using imshow() and pass the original image to it, turn off the x-axis labels, and specify the title in the title().

  • Lines 25–28: Display the image using imshow() and pass the blurred image to it, turn off the x-axis labels, and specify the title in the title().

  • Line 30: Show the resultant plot.

Apply box blur

Now let's blur the image more by applying a box blur. A box blur is an image-blurring technique that applies the concept of replacing the image pixel values with the mean value of the neighboring pixel. However, it uses a kernel to apply the mean value concept with the same value for all the pixels in a specified box-shaped area. This provides an even more smooth appearance.

Expected output

When a box blur is applied to the original picture, its overall smoothness improves, and the details of the image are not clearly visible.

Apply box blur on an image.
Apply box blur on an image.

Example code

In this example, we fetch an online image through its link address and apply a box blur effect by using ImageFilter.BoxBlur(6)) where 6 is the radius value.

import matplotlib.pyplot as plt
from PIL import Image, ImageFilter
import requests
from io import BytesIO

image_url = "https://images.pexels.com/photos/699466/pexels-photo-699466.jpeg?auto=compress&cs=tinysrgb&w=300"

#fetch image
response = requests.get(image_url)
image_data = BytesIO(response.content)
original_image = Image.open(image_data)

# Apply box blur
blur_image = original_image.filter(ImageFilter.BoxBlur(6)) 

plt.figure(figsize=(10, 5))
plt.subplots_adjust(wspace=0.4, hspace=0.1)


plt.subplot(1, 2, 1)
plt.imshow(original_image)
plt.axis('off')
plt.title('Original Image')

plt.subplot(1, 2, 2) 
plt.imshow(blur_image)
plt.axis('off')
plt.title('Box blurred Image')

plt.show()
box blur image.

Code explanation

  • Lines 1–4: Import the required libraries and modules.

  • Line 6: Store the link of the image that is to be used on the image_url. We can also use a local image file and give its exact name.

  • Lines 9–11: Use request to fetch the image from the server and open it using the Image.open() method and pass the converted image to it.

  • Line 14: Use the filter method of Image class to apply the BoxBlur() and pass the radius value as a parameter.

  • Lines 16–17: Specify the figure size that is to appear in the plot and assign the grid size.

  • Lines 20–23: Display the image using imshow() and pass the original image to it, turn off the x-axis labels, and specify the title in the title().

  • Lines 25–28: Display the image using imshow() and pass the blurred image to it, turn off the x-axis labels, and specify the title in the title().

  • Line 30: Show the resultant plot.

Apply Gaussian blur

Now let's achieve a smoother appearance and reduce the noise by applying Gaussian blur. A Gaussian blur is an image-blurring technique that applies a Gaussian distribution function on the pixels in the specified kernel area. This approach gives more weight to the pixels in the center, enabling a blur effect without losing the image definition.

Expected output

When a Gaussian blur is applied to the original picture, the details of the image are not visible, and a more smooth image is because the noise is reduced, but notice that the edges are still preserved.

Apply gaussian blur on an image.
Apply gaussian blur on an image.

Example code

In this example, we fetch an online image through its link address and apply a Gaussian blur effect by using ImageFilter.GaussianBlur(6)) where 6 is the radius value.

import matplotlib.pyplot as plt
from PIL import Image, ImageFilter
import requests
from io import BytesIO

image_url = "https://images.pexels.com/photos/699466/pexels-photo-699466.jpeg?auto=compress&cs=tinysrgb&w=300"

#fetch image
response = requests.get(image_url)
image_data = BytesIO(response.content)
original_image = Image.open(image_data)

#Apply guassian blur
blur_image = original_image.filter(ImageFilter.GaussianBlur(6)) 

plt.figure(figsize=(10, 5))
plt.subplots_adjust(wspace=0.4, hspace=0.1) 


plt.subplot(1, 2, 1)  
plt.imshow(original_image)
plt.axis('off')
plt.title('Original Image')

plt.subplot(1, 2, 2)  
plt.imshow(blur_image)
plt.axis('off')
plt.title('Blurred Image')

plt.show()
Gaussian blur image.

Code explanation

  • Lines 1–4: Import the required libraries and modules.

  • Line 6: Store the link of the image that is to be used on the image_url. We can also use a local image file and give its exact name.

  • Lines 9–11: Use request to fetch the image from the server and open it using the Image.open() method and pass the converted image to it.

  • Line 14: Use the filter method of Image class to apply the GaussianBlur() and pass the radius value as a parameter.

  • Lines 16–17: Specify the figure size that is to appear in the plot and assign the grid size.

  • Lines 20–23: Display the image using imshow() and pass the original image to it, turn off the x-axis labels, and specify the title in the title().

  • Lines 25–28: Display the image using imshow() and pass the blurred image to it, turn off the x-axis labels, and specify the title in the title().

  • Line 30: Show the resultant plot.

Real-life application

There are a lot of real-life scenarios where there is a need to blur an image. Let's take a look at few of the scenarios where blurring an image is useful.

Real life applications of a blurred image.
Real life applications of a blurred image.

Summary

Image blur is one of the commonly used filter effects when it comes to images and can be helpful to cater to a lot of real-life needs. We can apply a simple, box, and Gaussian blur and achieve different kinds of blur effects depending on the requirement.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved