This article mainly introduces how to use the PIL module in Python to perform Gaussian blur processing on images. this graphic script code is very simple, you can see from an article that PIL 1.1.5 has built-in Gaussian blur, but it is not mentioned in the document, and radius is hard-coded in Gaussian blur of PIL, although the radius parameter is passed in the constructor, it is not used at all (see here). Therefore, you need to modify it yourself. of course, knowing the cause makes the modification very easy.
According to the requirements in the post, perform Gaussian blur on the local part. Therefore, use the crop and paste methods to implement local filter.
The code is as follows:
#-*-Coding: UTF-8-*-from PIL import Image, ImageFilterclass MyGaussianBlur (ImageFilter. filter): name = "GaussianBlur" def _ init _ (self, radius = 2, bounds = None): self. radius = radius self. bounds = bounds def filter (self, image): if self. bounds: clips = image. crop (self. bounds ). gaussian_blur (self. radius) image. paste (clips, self. bounds) return image else: return image. gaussian_blur (self. radius) bounds = (150,130,280,230) image = Image.open('source.jpg ') image = image. filter (MyGaussianBlur (radius = 29, bounds = bounds) image. show ()
You can see the following results: