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

作者:PHILOS_THU 时间:2022-11-13 08:36:56 

本文实例讲述了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程序设计有所帮助。

来源:https://blog.csdn.net/guduruyu/article/details/71439733

标签:Python,图像,合成
0
投稿

猜你喜欢

  • 解析mysql 缓存如何使用内存

    2024-01-19 05:21:32
  • 英文版面设计的8个禁忌

    2009-10-14 20:42:00
  • 很无聊的一个找碴游戏

    2008-07-02 13:10:00
  • python处理Excel xlrd的简单使用

    2023-11-29 04:13:32
  • 解决pytorch 交叉熵损失输出为负数的问题

    2022-04-29 10:17:33
  • 如何在vue中使用kindeditor富文本编辑器

    2024-05-09 15:16:26
  • python+selenium实现163邮箱自动登陆的方法

    2021-01-24 08:05:15
  • Pytorch修改ResNet模型全连接层进行直接训练实例

    2023-06-07 03:49:28
  • 浅谈用VSCode写python的正确姿势

    2022-11-20 20:52:06
  • Python自动化测试利器selenium详解

    2021-06-07 20:22:10
  • SQL Server 2019自定义安装教程

    2024-01-12 21:14:22
  • 清理Mysql general_log的方法总结

    2024-01-14 10:54:34
  • 对python中使用requests模块参数编码的不同处理方法

    2022-11-02 01:30:29
  • Javascript typeof 用法

    2013-10-20 20:49:40
  • Python读取txt某几列绘图的方法

    2021-07-27 06:29:52
  • 浅谈Python之Django(三)

    2021-07-07 00:38:25
  • Python真题案例之二分法查找详解

    2023-09-23 01:39:07
  • go install和go get的区别实例详解

    2024-05-05 09:30:07
  • Python数据分析之使用matplotlib绘制折线图、柱状图和柱线混合图

    2023-09-16 23:18:09
  • python读取pdf格式文档的实现代码

    2023-12-10 07:25:49
  • asp之家 网络编程 m.aspxhome.com