This article describes how to use the PIL Library in Python to implement image Gaussian blur. This article focuses on modifying the Pil source code to customize the blur. For more information, see
1. install PIL
PIL is short for Python Imaging Library and is used to process images. PIL already has image Gaussian blur processing class, but there is a bug (the latest 1.1.7bug still exists), that is, the blur radius is written to 2 and cannot be set. In line 2 of source code ImageFilter. py:
So we can change it here.
Project address: http://www.pythonware.com/products/pil/
II. modified code
The code is as follows:
The code is as follows:
#-*-Coding: UTF-8 -*-
From PIL import Image, ImageFilter
Class 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)
III. Call
The code is as follows:
Simg = 'demo.jpg'
Dimg = 'demo_blur.jpg'
Image = Image. open (simg)
Image = image. filter (MyGaussianBlur (radius = 30 ))
Image. save (dimg)
Print dimg, 'success'
If you only need to process a region, pass the bounds parameter.
IV. Effect
SOURCE image:
After processing: