2、合并两张图像

目录

CV实现

PIL实现


使用一个函数实现两张图像合并,通过参数指定合并方式(水平或垂直或加权)。

CV实现

复制代码
import cv2
import numpy as np

def merge_images_cv(image_path1, image_path2, method='horizontal', alpha=0.5):
    # 读取图像
    img1 = cv2.imread(image_path1)
    img2 = cv2.imread(image_path2)

    # 确保 alpha 在 0 和 1 之间
    alpha = max(0, min(alpha, 1))

    if method == 'horizontal':
        # 调整 img2 到 img1 的高度
        img2 = cv2.resize(img2, (img2.shape[1], img1.shape[0]))
        # 水平合并
        merged_img = np.hstack((img1, img2))
        # merged_img = np.concatenate((image1, image2), axis=1)
    elif method == 'vertical':
        # 调整 img2 到 img1 的宽度
        img2 = cv2.resize(img2, (img1.shape[1], img2.shape[0]))
        # 垂直合并
        merged_img = np.vstack((img1, img2))
        # merged_img = np.concatenate((image1, image2), axis=0)
    elif method == 'weighted':
        # 加权合并,确保两张图像大小相同
        img2 = cv2.resize(img2, (img1.shape[1], img1.shape[0]))
        # 加权合并
        merged_img = cv2.addWeighted(img1, alpha, img2, 1 - alpha, 0)
    else:
        raise ValueError("Method must be 'horizontal', 'vertical', or 'weighted'")

    return merged_img

# 使用示例
merged_image = merge_images_cv('path_to_first_image.jpg', 'path_to_second_image.jpg', method='horizontal')
cv2.imwrite('merged_image.jpg', merged_image)

PIL实现

复制代码
from PIL import Image

def merge_images_pil(image_path1, image_path2, method='horizontal', alpha=0.5):
    # 打开图像
    img1 = Image.open(image_path1)
    img2 = Image.open(image_path2)

    # 确保 alpha 在 0 和 1 之间
    alpha = max(0, min(alpha, 1))

    if method == 'horizontal':
        # 调整 img2 到 img1 的高度
        img2 = img2.resize((img2.width, img1.height))
        # 水平合并
        merged_img = Image.new('RGB', (img1.width + img2.width, img1.height))
        merged_img.paste(img1, (0, 0))
        merged_img.paste(img2, (img1.width, 0))
    elif method == 'vertical':
        # 调整 img2 到 img1 的宽度
        img2 = img2.resize((img1.width, img2.height))
        # 垂直合并
        merged_img = Image.new('RGB', (img1.width, img1.height + img2.height))
        merged_img.paste(img1, (0, 0))
        merged_img.paste(img2, (0, img1.height))
    elif method == 'weighted':
        # 加权合并,确保两张图像大小相同
        img2 = img2.resize((img1.width, img1.height))
        # 加权合并
        merged_img = Image.blend(img1, img2, alpha)
    else:
        raise ValueError("Method must be 'horizontal', 'vertical', or 'weighted'")

    return merged_img

# 使用示例
merged_image = merge_images_pil('path_to_first_image.jpg', 'path_to_second_image.jpg', method='horizontal')
merged_image.save('merged_image.jpg')
相关推荐
格林威29 分钟前
常规线扫描镜头有哪些类型?能做什么?
人工智能·深度学习·数码相机·算法·计算机视觉·视觉检测·工业镜头
倔强青铜三1 小时前
苦练Python第63天:零基础玩转TOML配置读写,tomllib模块实战
人工智能·python·面试
B站计算机毕业设计之家2 小时前
智慧交通项目:Python+YOLOv8 实时交通标志系统 深度学习实战(TT100K+PySide6 源码+文档)✅
人工智能·python·深度学习·yolo·计算机视觉·智慧交通·交通标志
高工智能汽车2 小时前
棱镜观察|极氪销量遇阻?千里智驾左手服务吉利、右手对标华为
人工智能·华为
txwtech2 小时前
第6篇 OpenCV RotatedRect如何判断矩形的角度
人工智能·opencv·计算机视觉
正牌强哥2 小时前
Futures_ML——机器学习在期货量化交易中的应用与实践
人工智能·python·机器学习·ai·交易·akshare
倔强青铜三2 小时前
苦练Python第62天:零基础玩转CSV文件读写,csv模块实战
人工智能·python·面试
大模型真好玩2 小时前
低代码Agent开发框架使用指南(二)—Coze平台核心功能概览
人工智能·coze·deepseek
jerryinwuhan3 小时前
最短路径问题总结
开发语言·人工智能·python
wanhengidc3 小时前
云手机能够做些什么?
运维·服务器·人工智能·智能手机·云计算