Python图像处理实现两幅图像合成一幅图像的方法【测试可用】
作者:PHILOS_THU 时间:2022-11-13 08:36:56
本文实例讲述了Python图像处理实现两幅图像合成一幅图像的方法。分享给大家供大家参考,具体如下:
将两幅图像合成一幅图像,是图像处理中常用的一种操作,python图像处理库PIL中提供了多种种将两幅图像合成一幅图像的接口。
下面我们通过不同的方式,将两图合并成一幅图像。
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 进行。
合成结果如下:
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程序设计有所帮助。
来源:https://blog.csdn.net/guduruyu/article/details/71439733
标签:Python,图像,合成
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解析mysql 缓存如何使用内存
2024-01-19 05:21:32
英文版面设计的8个禁忌
2009-10-14 20:42:00
![](https://img.aspxhome.com/file/UploadPic/200910/14/01-84s.jpg)
很无聊的一个找碴游戏
2008-07-02 13:10:00
python处理Excel xlrd的简单使用
2023-11-29 04:13:32
解决pytorch 交叉熵损失输出为负数的问题
2022-04-29 10:17:33
![](https://img.aspxhome.com/file/2023/4/103744_0s.jpg)
如何在vue中使用kindeditor富文本编辑器
2024-05-09 15:16:26
![](https://img.aspxhome.com/file/2023/0/126510_0s.png)
python+selenium实现163邮箱自动登陆的方法
2021-01-24 08:05:15
![](https://img.aspxhome.com/file/2023/7/95597_0s.gif)
Pytorch修改ResNet模型全连接层进行直接训练实例
2023-06-07 03:49:28
浅谈用VSCode写python的正确姿势
2022-11-20 20:52:06
![](https://img.aspxhome.com/file/2023/0/126930_0s.gif)
Python自动化测试利器selenium详解
2021-06-07 20:22:10
![](https://img.aspxhome.com/file/2023/1/112951_0s.png)
SQL Server 2019自定义安装教程
2024-01-12 21:14:22
![](https://img.aspxhome.com/file/2023/4/94434_0s.jpg)
清理Mysql general_log的方法总结
2024-01-14 10:54:34
对python中使用requests模块参数编码的不同处理方法
2022-11-02 01:30:29
![](https://img.aspxhome.com/file/2023/8/103688_0s.jpg)
Javascript typeof 用法
2013-10-20 20:49:40
Python读取txt某几列绘图的方法
2021-07-27 06:29:52
浅谈Python之Django(三)
2021-07-07 00:38:25
![](https://img.aspxhome.com/file/2023/3/77743_0s.png)
Python真题案例之二分法查找详解
2023-09-23 01:39:07
![](https://img.aspxhome.com/file/2023/6/85376_0s.png)
go install和go get的区别实例详解
2024-05-05 09:30:07
Python数据分析之使用matplotlib绘制折线图、柱状图和柱线混合图
2023-09-16 23:18:09
![](https://img.aspxhome.com/file/2023/5/93625_0s.png)
python读取pdf格式文档的实现代码
2023-12-10 07:25:49
![](https://img.aspxhome.com/file/2023/9/133559_0s.png)