SoFunction
Updated on 2024-11-19

Example of using the PIL library in Python to implement Gaussian blurring of images

I. Installation of PIL

PIL is short for Python Imaging Library, which is used to process images.PIL already has a class for Gaussian blur processing of images, but there is a bug (the latest 1.1.7 bug still exists), which is that the blur radius is written to be 2, and cannot be set. It's in line 160 of the source code:

So, we'll just change it ourselves here and it'll be OK.

Project Address:/products/pil/

II. Modified code

The code is as follows:

Copy Code The code is as follows.

#-*- coding: utf-8 -*-

from PIL import Image, ImageFilter

class MyGaussianBlur():
    name = "GaussianBlur"

    def __init__(self, radius=2, bounds=None):
        = radius
        = bounds

    def filter(self, image):
        if :
            clips = ().gaussian_blur()
            (clips, )
            return image
        else:
            return image.gaussian_blur()

III. Calls

Copy Code The code is as follows.

simg = ''
dimg = 'demo_blur.jpg'
image = (simg)
image = (MyGaussianBlur(radius=30))
(dimg)
print dimg, 'success'

If you only need to process a certain area, just pass in the bounds parameter

IV. Effects
Original image:

Processed: