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


Posted in Python onFebruary 08, 2015

一、安装PIL

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

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

所以,我们在这里自己改一下就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库实现图片高斯模糊实例

处理后的:

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

Python 相关文章推荐
python生成式的send()方法(详解)
May 08 Python
Python实现的密码强度检测器示例
Aug 23 Python
python如何为被装饰的函数保留元数据
Mar 21 Python
python3.4.3下逐行读入txt文本并去重的方法
Apr 29 Python
JSON文件及Python对JSON文件的读写操作
Oct 07 Python
django 微信网页授权登陆的实现
Jul 30 Python
YUV转为jpg图像的实现
Dec 09 Python
Python监控服务器实用工具psutil使用解析
Dec 19 Python
django在保存图像的同时压缩图像示例代码详解
Feb 11 Python
详解numpy1.19.4与python3.9版本冲突解决
Dec 15 Python
python 基于opencv去除图片阴影
Jan 26 Python
win10+anaconda安装yolov5的方法及问题解决方案
Apr 29 Python
Python中解析JSON并同时进行自定义编码处理实例
Feb 08 #Python
Python Web框架Flask中使用七牛云存储实例
Feb 08 #Python
Python Web框架Flask中使用百度云存储BCS实例
Feb 08 #Python
Python Web框架Flask中使用新浪SAE云存储实例
Feb 08 #Python
Python中装饰器的一个妙用
Feb 08 #Python
Python中使用HTMLParser解析html实例
Feb 08 #Python
Pyhton中防止SQL注入的方法
Feb 05 #Python
You might like
用PHP+java实现自动新闻滚动窗口
2006/10/09 PHP
PHP和javascript常用正则表达式及用法实例
2014/07/01 PHP
将PHP从5.3.28升级到5.3.29时Nginx出现502错误
2015/05/09 PHP
PHP页面输出时js设置input框的选中值
2016/09/30 PHP
thinkPHP5.0框架引入Traits功能实例分析
2017/03/18 PHP
Laravel框架自定义公共函数的引入操作示例
2019/04/16 PHP
PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】
2019/12/20 PHP
Javascript的各种节点操作实例演示代码
2012/06/27 Javascript
JS获取文件大小方法小结
2015/12/08 Javascript
javascript轮播图算法
2016/10/21 Javascript
jQuery双向列表选择器select版
2016/11/01 Javascript
input框中的name和id的区别
2016/11/16 Javascript
基于Vue.js 2.0实现百度搜索框效果
2020/12/28 Javascript
JavaScript内置对象math,global功能与用法实例分析
2019/06/10 Javascript
js+canvas实现纸牌游戏
2020/03/16 Javascript
python根据经纬度计算距离示例
2014/02/16 Python
python获取Linux下文件版本信息、公司名和产品名的方法
2014/10/05 Python
在Python 3中实现类型检查器的简单方法
2015/07/03 Python
python实现连续图文识别
2018/12/18 Python
网易2016研发工程师编程题 奖学金(python)
2019/06/19 Python
python的pytest框架之命令行参数详解(上)
2019/06/27 Python
10分钟用python搭建一个超好用的CMDB系统
2019/07/17 Python
pytorch在fintune时将sequential中的层输出方法,以vgg为例
2019/08/20 Python
windows+vscode安装paddleOCR运行环境的步骤
2020/11/11 Python
举例讲解Python装饰器
2020/12/24 Python
英国泰坦旅游网站:全球陪同游览,邮轮和铁路旅行
2016/11/29 全球购物
美国爆米花工厂:The Popcorn Factory
2019/09/14 全球购物
感恩节活动方案
2014/01/27 职场文书
施工安全责任书
2014/04/14 职场文书
敬老院活动总结
2014/04/28 职场文书
学习保证书范文
2014/04/30 职场文书
新闻编辑求职信
2014/07/13 职场文书
税务干部个人整改措施思想汇报
2014/10/10 职场文书
干部考核工作总结
2015/08/12 职场文书
初中生物教学随笔
2015/08/15 职场文书
vue.js 使用原生js实现轮播图
2022/04/26 Vue.js