Box blur algorithm implementation in Python

In this tutorial, we will learn about Box blur algorithm implementation in Python using Pillow(PIL) library.

Box Blur algorithm using Pillow(PIL) library

What is blur box algorithm ?

Blur Box Algorithm is Image Processing technique. Every Image contains many pixels with different pixel values. The Box Blue Algorithm operates by replacing the average value of the surroundings pixels with each pixel in the image. This will effectively  blur the image.

What is Pillow(PIL) Library ?

Pillow is a popular and powerful tool in python Imaging Library (PIL). This tool allows the users for opening , manipulating and saving the images of different file formats.

Pillow supports various image processing tasks such as image enhancement , manipulation, file handling and drawing on images.

Pillow or PIL library is an image processing library in Python that processing image data and provides images as required.

In this, we will blur the image using Box filter. The ImageFilter is a class in the Python Pillow(PIL) library that contains a function called BoxBlur() . The BoxBlur() function used to apply the box blur filter and it takes only one parameter called blur radius.

Example :

Algorithm 

  • Import Image and Image Filter from PIL.
  • Open the image.
  • Call the boxblur() function and specify the radius. Here averaging of the pixel values with respect to a given radius takes place.
  • Display the output or we can also save the image depending upon the user’s requirement.

CODE :

from PIL import Image
import numpy as np

def box_blur(image_path, kernel_size):
    img = Image.open(image_path)
    img = img.convert("RGB")
    
    img_array = np.array(img)
    height, width, channels = img_array.shape
    
    blurred_array = np.zeros((height, width, channels), dtype = np.uint8)
    offset = kernel_size // 2
    
    for y in range(height):
        for x in range(width): 
            sum_r = sum_g = sum_b = 0
            count = 0

            for ky in range(-offset, offset + 1):
                for kx in range(-offset, offset + 1):
                    ny = y + ky
                    nx = x + kx
            if 0 <= ny < height and 0 <= nx < width:
                 r, g, b = img_array[ny, nx]
                 sum_r += r
                 sum_g += g
                 sum_b += b
                 count += 1
                    
            blurred_array[y, x] = (sum_r // count, sum_g // count, sum_b // count)
  
    blurred_img = Image.fromarray(blurred_array)
    return blurred_img 

#Download the image and save it as 'eiffel_tower.jpg' on your local machine 
eiffel_tower_img_path = "eiffel_tower.jpg"
  
blurred_image = box_blur("input_image.jpg", 3)
blurred_image.show()
blurred_image.save("blurred_image.jpg")

 

ImageFilter:

from PIL import ImageFilter

blurred_image = image.filter(ImageFilter.BLUR)
blurred_image.show() 

 

Explanation :

In the above code, we imported the ImageFilter function from PIL. ImageFilter function allows the user to blur the image. This is one of the way in which we can blur a image.

BoxBlur() :

from PIL import Image, ImageFilter

im1 = Image.open(r"C:\Users\sadow984\Desktop\download2.JPG")


im2 = im1.filter(ImageFilter.BoxBlur(7))

im2.show()

 

Explanation :

In the above code, we used the BoxBlur() function that is imported from the PIL library.
We imported the Image from functions like opening, showing and saving the image. We imported the ImageFilter module to use BoxBlur() function.

OUTPUT :

 

    Original Image

 

 

Blur image

Blur image

 

 

 

 

 

 

 

 

Leave a Reply

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