手机
当前位置:查字典教程网 >脚本专栏 >python >Python中使用PIL库实现图片高斯模糊实例
Python中使用PIL库实现图片高斯模糊实例
摘要:一、安装PILPIL是PythonImagingLibrary简称,用于处理图片。PIL中已经有图片高斯模糊处理类,但有个bug(目前最新的...

一、安装PIL

PIL是Python Imaging Library简称,用于处理图片。PIL中已经有图片高斯模糊处理类,但有个bug(目前最新的1.1.7bug还存在),就是模糊半径写死的是2,不能设置。在源码ImageFilter.py的第160行:

Python中使用PIL库实现图片高斯模糊实例1

所以,我们在这里自己改一下就OK了。

项目地址:http://www.pythonware.com/products/pil/

二、修改后的代码

代码如下:

复制代码 代码如下:

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

三、调用

复制代码 代码如下:

simg = 'demo.jpg'

dimg = 'demo_blur.jpg'

image = Image.open(simg)

image = image.filter(MyGaussianBlur(radius=30))

image.save(dimg)

print dimg, 'success'

如果只需要处理某个区域,传入bounds参数即可

四、效果

原图:

Python中使用PIL库实现图片高斯模糊实例2

处理后的:

Python中使用PIL库实现图片高斯模糊实例3

【Python中使用PIL库实现图片高斯模糊实例】相关文章:

Python中使用PyHook监听鼠标和键盘事件实例

python使用新浪微博api上传图片到微博示例

在python中的socket模块使用代理实例

python三元运算符实现方法

python实现保存网页到本地示例

Python urlopen 使用小示例

python使用rabbitmq实现网络爬虫示例

Python使用代理抓取网站图片(多线程)

python实现博客文章爬虫示例

python使用xmlrpc实例讲解

精品推荐
分类导航