Python图像处理实现两幅图像合成一幅图像的方法【测试可用】


Posted in Python onJanuary 04, 2019

本文实例讲述了Python图像处理实现两幅图像合成一幅图像的方法。分享给大家供大家参考,具体如下:

将两幅图像合成一幅图像,是图像处理中常用的一种操作,python图像处理库PIL中提供了多种种将两幅图像合成一幅图像的接口。

下面我们通过不同的方式,将两图合并成一幅图像。

Python图像处理实现两幅图像合成一幅图像的方法【测试可用】

Python图像处理实现两幅图像合成一幅图像的方法【测试可用】

1、使用Image.blend()接口

代码如下:

# -*- coding:utf-8 -*-
from PIL import Image
def blend_two_images():
  img1 = Image.open( "bridge.png ")
  img1 = img1.convert('RGBA')
  img2 = Image.open( "birds.png ")
  img2 = img2.convert('RGBA')
  img = Image.blend(img1, img2, 0.3)
  img.show()
  img.save( "blend.png")
  return
blend_two_images()

两幅图像进行合并时,按公式:blended_img = img1 * (1 ? alpha) + img2* alpha 进行。

合成结果如下:

Python图像处理实现两幅图像合成一幅图像的方法【测试可用】

2、使用Image.composite()接口

该接口使用掩码(mask)的形式对两幅图像进行合并。

代码如下:

# -*- coding:utf-8 -*-
from PIL import Image
def blend_two_images2():
  img1 = Image.open( "bridge.png ")
  img1 = img1.convert('RGBA')
  img2 = Image.open( "birds.png ")
  img2 = img2.convert('RGBA')
  r, g, b, alpha = img2.split()
  alpha = alpha.point(lambda i: i>0 and 204)
  img = Image.composite(img2, img1, alpha)
  img.show()
  img.save( "blend2.png")
  return
blend_two_images2()

代码第9行中指定的204起到的效果和使用blend()接口时的0.3类似。

合并后的效果如下:

Python图像处理实现两幅图像合成一幅图像的方法【测试可用】

更多关于Python相关内容可查看本站专题:《Python数学运算技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python random模块常用方法
Nov 03 Python
Python中使用第三方库xlutils来追加写入Excel文件示例
Apr 05 Python
MySQL最常见的操作语句小结
May 07 Python
Django中使用locals()函数的技巧
Jul 16 Python
python3.4用循环往mysql5.7中写数据并输出的实现方法
Jun 20 Python
python验证码识别教程之利用投影法、连通域法分割图片
Jun 04 Python
Django框架登录加上验证码校验实现验证功能示例
May 23 Python
Flask模板引擎之Jinja2语法介绍
Jun 26 Python
利用python-pypcap抓取带VLAN标签的数据包方法
Jul 23 Python
python字符串格式化方式解析
Oct 19 Python
TensorFlow dataset.shuffle、batch、repeat的使用详解
Jan 21 Python
使用Python制作一盏 3D 花灯喜迎元宵佳节
Feb 26 Python
Python小游戏之300行代码实现俄罗斯方块
Jan 04 #Python
django主动抛出403异常的方法详解
Jan 04 #Python
pyspark操作MongoDB的方法步骤
Jan 04 #Python
详解Appium+Python之生成html测试报告
Jan 04 #Python
python虚拟环境迁移方法
Jan 03 #Python
对django xadmin自定义菜单的实例详解
Jan 03 #Python
在Python中关于使用os模块遍历目录的实现方法
Jan 03 #Python
You might like
PHP iconv 解决utf-8和gb2312编码转换问题
2010/04/12 PHP
php把数据表导出为Excel表的最简单、最快的方法(不用插件)
2014/05/10 PHP
thinkphp区间查询、统计查询与SQL直接查询实例分析
2014/11/24 PHP
php中memcache 基本操作实例
2015/05/17 PHP
Ajax PHP JavaScript MySQL实现简易无刷新在线聊天室
2016/08/17 PHP
php使用glob函数遍历文件和目录详解
2016/09/23 PHP
thinkphp5 + ajax 使用formdata提交数据(包括文件上传) 后台返回json完整实例
2020/03/02 PHP
JSON 教程 json入门学习笔记
2020/09/22 Javascript
基于jquery的商品展示放大镜
2010/08/07 Javascript
如何将JS的变量值传递给ASP变量
2012/12/10 Javascript
javascript实现div的拖动并调整大小类似qq空间个性编辑模块
2012/12/12 Javascript
jquery Mobile入门—外部链接切换示例代码
2013/01/08 Javascript
jqgrid 编辑添加功能详细解析
2013/11/08 Javascript
jQuery源码解读之removeClass()方法分析
2015/02/20 Javascript
Javascript简单实现面向对象编程继承实例代码
2015/11/27 Javascript
jQuery中库的引用方法
2018/01/06 jQuery
详解Vue依赖收集引发的问题
2019/04/22 Javascript
js实现时分秒倒计时
2019/12/03 Javascript
tensorflow学习笔记之mnist的卷积神经网络实例
2018/04/15 Python
python3 json数据格式的转换(dumps/loads的使用、dict to str/str to dict、json字符串/字典的相互转换)
2019/04/01 Python
python for循环remove同一个list过程解析
2019/08/14 Python
python读取ini配置文件过程示范
2019/12/23 Python
tensorflow mnist 数据加载实现并画图效果
2020/02/05 Python
python GUI库图形界面开发之PyQt5单选按钮控件QRadioButton详细使用方法与实例
2020/02/28 Python
Python的in,is和id函数代码实例
2020/04/18 Python
keras实现多种分类网络的方式
2020/06/11 Python
KIKO比利时官网:意大利彩妆品牌
2017/07/23 全球购物
iHerb台湾:维生素、保健品和健康产品
2018/01/31 全球购物
Lampegiganten丹麦:欧洲领先的照明网上商店
2018/04/25 全球购物
施华洛世奇加拿大官网:SWAROVSKI加拿大
2018/06/03 全球购物
验房委托书
2014/08/30 职场文书
后进基层党组织整改方案
2014/10/25 职场文书
2014年学校法制宣传日活动总结
2014/11/01 职场文书
关于观后感的作文
2015/06/18 职场文书
初中开学典礼新闻稿
2015/07/17 职场文书
高中语文教学反思范文
2016/02/16 职场文书