從一篇文章中看到,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()
可以看下效果:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
