What is BoxBlur in Python?
The PIL library
Pillow, PIL, or the Python Imaging Library is an image processing library in Python. This library supports a wide range of file formats, is designed for an efficient internal representation of image data, and provides powerful image processing capabilities.
The ImageFilter module
The ImageFilter module in PIL contains definitions for a pre-defined set of filters that are used for different purposes.
The BoxBlur function
The BoxBlur filter function sets each pixel to the average value of the pixels in a square box. It extends the radius pixels in each direction to blur the image.
Syntax
PIL.ImageFilter.BoxBlur(radius=2)
Parameter
radius: This is the size of the box in one direction. A zero radius does not blur the image.
Example
from PIL import Image, ImageFilterimport matplotlib.pyplot as pltimg = Image.open("man.png")img_blur = img.filter(ImageFilter.BoxBlur(4))img_blur.save("man_blur.png")
Explanation
- Line 1: We import the
ImageFilterandImagemodules. - Line 3:
man.pngis read into memory usingopen()in theImagemodule. - Line 5: We apply the
BoxBlurfilter with the radius4to the image we load in Line 3. - Line 7: We then save the blurred image.