python 给图像添加透明度(alpha通道)


Posted in Python onApril 09, 2020

我们常见的RGB图像通常只有R、G、B三个通道,在图像处理的过程中会遇到往往需要向图像中添加透明度信息,如公司logo的设计,其输出图像文件就需要添加透明度,即需要在RGB三个通道的基础上添加alpha通道信息。这里介绍两种常见的向RGB图像中添加透明度的方法。

1、使用图像合成(blending)的方法

可参考上篇博文(python图像处理(十)——两幅图像的合成一幅图像(blending two images) )

代码如下:

#-*- coding: UTF-8 -*- 
 
from PIL import Image
 
def addTransparency(img, factor = 0.7 ):
  img = img.convert('RGBA')
  img_blender = Image.new('RGBA', img.size, (0,0,0,0))
  img = Image.blend(img_blender, img, factor)
  return img
 
 
img = Image.open( "SMILEY.png ")
img = addTransparency(img, factor =0.7)

这里给原图的所有像素都添加了一个常量(0.7)的透明度。

处理前后的效果如下:

python 给图像添加透明度(alpha通道)

2、使用Image对象的成员函数putalpha()直接添加

代码如下:

#-*- coding: UTF-8 -*- 
 
from PIL import Image
 
img = Image.open("SMILEY.png ")
img = img.convert('RGBA')
r, g, b, alpha = img.split()
alpha = alpha.point(lambda i: i>0 and 178)
img.putalpha(alpha)

处理前后的效果如下:

python 给图像添加透明度(alpha通道) 

到此这篇关于python 给图像添加透明度(alpha通道)的文章就介绍到这了,更多相关python  图像添加透明度内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python简单实现Base64编码和解码的方法
Apr 29 Python
python中日志logging模块的性能及多进程详解
Jul 18 Python
python爬虫爬取网页表格数据
Mar 07 Python
使用Django和Python创建Json response的方法
Mar 26 Python
TensorFlow 滑动平均的示例代码
Jun 19 Python
pycharm重置设置,恢复默认设置的方法
Oct 22 Python
python多任务及返回值的处理方法
Jan 22 Python
python实现最大子序和(分治+动态规划)
Jul 05 Python
python读取csv文件指定行的2种方法详解
Feb 13 Python
python多线程实现同时执行两个while循环的操作
May 02 Python
Python正则表达式如何匹配中文
May 27 Python
python单向链表实例详解
May 25 Python
Python日志logging模块功能与用法详解
Apr 09 #Python
解决python DataFrame 打印结果不换行问题
Apr 09 #Python
Python的PIL库中getpixel方法的使用
Apr 09 #Python
Python 随机生成测试数据的模块:faker基本使用方法详解
Apr 09 #Python
python图片指定区域替换img.paste函数的使用
Apr 09 #Python
python程序输出无内容的解决方式
Apr 09 #Python
python爬虫学习笔记之Beautifulsoup模块用法详解
Apr 09 #Python
You might like
超级好用的一个php上传图片类(随机名,缩略图,加水印)
2010/06/30 PHP
thinkphp3查询mssql数据库乱码解决方法分享
2014/02/11 PHP
php 流程控制switch的简单实例
2016/06/07 PHP
php-msf源码详解
2017/12/25 PHP
PHP的PDO大对象(LOBs)
2019/01/27 PHP
Nigma vs Liquid BO3 第一场2.14
2021/03/10 DOTA
JS 判断代码全收集
2009/04/28 Javascript
JavaScript在多浏览器下for循环的使用方法
2012/11/07 Javascript
jquery fancybox ie6不显示关闭按钮的解决办法
2013/12/25 Javascript
JS实现向表格行添加新单元格的方法
2015/03/30 Javascript
JS模态窗口返回值兼容问题的完美解决方法
2016/05/28 Javascript
JQuery学习总结【一】
2016/12/01 Javascript
react 创建单例组件的方法
2018/04/26 Javascript
监听element-ui table滚动事件的方法
2019/03/26 Javascript
ES6 Object方法扩展的应用实例分析
2019/06/25 Javascript
javascript实现简单打字游戏
2019/10/29 Javascript
[39:19]完美世界DOTA2联赛PWL S2 SZ vs LBZS 第二场 11.26
2020/11/30 DOTA
Python实现的批量下载RFC文档
2015/03/10 Python
pygame学习笔记(5):游戏精灵
2015/04/15 Python
详解在Python和IPython中使用Docker
2015/04/28 Python
Python中防止sql注入的方法详解
2017/02/25 Python
Python之py2exe打包工具详解
2017/06/14 Python
python 寻找优化使成本函数最小的最优解的方法
2017/12/28 Python
TensorFlow 合并/连接数组的方法
2018/07/27 Python
Python开发最牛逼的IDE——pycharm
2018/08/01 Python
在Python中,不用while和for循环遍历列表的实例
2019/02/20 Python
python使用opencv实现马赛克效果示例
2019/09/28 Python
Python time库基本使用方法分析
2019/12/13 Python
CSS3+font字体文件实现圆形半透明菜单具体步骤(图解)
2013/06/03 HTML / CSS
联想法国官方网站:Lenovo法国
2018/10/18 全球购物
波兰品牌鞋履在线商店:Eastend.pl
2020/01/11 全球购物
护理工作感言
2014/01/16 职场文书
镇副书记专题民主生活会对照检查材料思想汇报
2014/10/02 职场文书
教师个人发展总结
2015/02/11 职场文书
教师师德承诺书2016
2016/03/25 职场文书
CSS 伪元素::marker详解
2021/06/26 HTML / CSS