A tutorial on using the PIL module to Gaussian blur a picture in Python

Source: Internet
Author: User
As seen from an article, PIL 1.1.5 has built-in Gaussian blur, but is not mentioned in the document, and PIL Gaussian blur in the radius is hard-coded, although the construction method has the incoming radius parameters, but there is no use (see here), so need to transform themselves, of course, Knowing the reason, it's naturally very easy to change.

In combination with the requirements in the post, Gaussian Blur is performed locally, so it is also necessary to use the crop and paste methods to implement the 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 = ( 280, () image = Image.open (' source.jpg ') image = Image.filter (Mygaussianblur (radius=29, Bounds=bounds)) Image.show ()

You can look at the effect:

  • 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.