python图像模糊处理_在Python中使用PIL模块对图片进行高斯模糊处理的教程

从一篇文章中看到,PIL 1.1.5 已经内置了高斯模糊,但是并没有在文档中提及,而且PIL的高斯模糊中 radius 是硬编码, 虽然构造方法中有传入 radius 参数,但压根就没有用到 (看这里),所以需要自己进行改造,当然,知道了原因, 修改起来自然非常简单了。

结合帖子中的需求,对局部进行高斯模糊,所以还需要结合使用 crop和 paste 方法实现局部使用滤镜。

代码如下:

#-*- 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)

bounds = (150, 130, 280, 230)

image = Image.open('source.jpg')

image = image.filter(MyGaussianBlur(radius=29, bounds=bounds))

image.show()

可以看下效果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值