-->

How to define radius for blur with Python Pillow?

2020-08-17 06:49发布

问题:

I'm trying to blur an image with Pillow, using the ImageFilter as follows:

from PIL import ImageFilter
blurred_image = im.filter(ImageFilter.BLUR)

This works fine, except that it has a set radius which is way too small for me. I want to blur the image so much that it can be barely recognised anymore. In the docs I see that the radius is set to 2 by default, but I don't really understand how I can set it to a larger value?

Does anybody have any idea how I could increase the blur radius with Pillow? All tips are welcome!

回答1:

Image.filter() takes an ImageFilter so you can create an ImageFilter.GaussianBlur instance with whatever radius you want, passed in as a named argument.

blurred_image = im.filter(ImageFilter.GaussianBlur(radius=50))

You can even make it more concise like so:

blurred_image = im.filter(ImageFilter.GaussianBlur(50))