Python中使用PIL库实现图片高斯模糊实例
作者:junjie 时间:2023-12-09 14:12:20
一、安装PIL
PIL是Python Imaging Library简称,用于处理图片。PIL中已经有图片高斯模糊处理类,但有个bug(目前最新的1.1.7bug还存在),就是模糊半径写死的是2,不能设置。在源码ImageFilter.py的第160行:
所以,我们在这里自己改一下就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,高斯模糊
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
编写SQL需要注意的细节Checklist总结
2012-10-07 10:43:57
spring使用RedisTemplate操作Redis数据库
2024-01-16 03:32:28
![](https://img.aspxhome.com/file/2023/4/89644_0s.png)
js序列化和反序列化的使用讲解
2023-08-05 08:13:24
Python random库使用方法及异常处理方案
2023-10-07 13:43:46
![](https://img.aspxhome.com/file/2023/1/82701_0s.jpg)
Bootstrap 模态框多次显示后台提交多次BUG的解决方法
2024-04-28 09:39:14
将python运行结果保存至本地文件中的示例讲解
2024-01-03 04:01:52
Selenium常见异常解析及解决方案示范
2023-06-27 20:09:48
asp如何制作一个WAP手机的WML网页?
2010-06-29 21:21:00
把网页中的(电话,qq等数字)生成图片的ASP程序
2011-04-11 10:40:00
python中reduce()函数的使用方法示例
2021-08-09 12:32:53
![](https://img.aspxhome.com/file/2023/9/117969_0s.png)
微信小程序实现计算器(含历史记录)
2024-04-17 10:30:20
![](https://img.aspxhome.com/file/2023/2/136262_0s.jpg)
python进程类subprocess的一些操作方法例子
2021-08-26 16:21:35
python多线程编程方式分析示例详解
2023-05-13 04:56:01
python3 pandas 读取MySQL数据和插入的实例
2024-01-22 06:21:33
如何在SQL2000的查询中使用XML-Data?
2010-06-18 19:26:00
Python中的枚举函数enumerate()的具体用法
2022-06-03 13:33:31
![](https://img.aspxhome.com/file/2023/3/120603_0s.png)
Python 的赋值,浅拷贝和深拷贝详解
2023-08-18 12:28:04
![](https://img.aspxhome.com/file/2023/2/131702_0s.png)
Django实现跨域的2种方法
2021-11-27 00:15:42
Python 基础教程之包和类的用法
2021-10-31 16:58:19
![](https://img.aspxhome.com/file/2023/8/95768_0s.png)
python实现BP神经网络回归预测模型
2023-01-21 17:53:44
![](https://img.aspxhome.com/file/2023/2/98322_0s.png)