Python uses the PIL library to implement image Gaussian Blur instances,

Source: Internet
Author: User

Python uses the PIL library to implement image Gaussian Blur instances,

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:
Copy codeThe 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
Copy codeThe 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.