拼接不同文件夹中同名图片的方法

news/2024/7/21 4:10:01 标签: 图像处理, 图像拼接, python, vstack, hastack, cv

有时候为了方便对比不同文件夹中同名图片,需要拼接在一起,这里提供一个拼接方法,当然不同命文件也可以实现拼接,稍微改改就能实现

如下图,在文件夹中有五个文件夹中的图片需要拼接,拼接后的图片存放在img_hebing

代码如下,为了隐去了一些项目私密信息

import warnings
warnings.filterwarnings('ignore')
import matplotlib.pyplot as plt
from matplotlib import cm as CM
import matplotlib.gridspec as gridspec
import os
import glob
import numpy as np
import scipy.io as scio
from scipy.io import loadmat
from PIL import Image; import cv2
import torch
from torchvision import transforms

DATA_PATH = f"D:/TEST/image_test/"

img_path1 = glob.glob(os.path.join(DATA_PATH, "four_visualization_1", "*.png"))  
img_path2 = glob.glob(os.path.join(DATA_PATH, "four_visualization_2", "*.png"))  
img_path3 = glob.glob(os.path.join(DATA_PATH, "four_visualization_3", "*.png"))  
img_path4 = glob.glob(os.path.join(DATA_PATH, "four_visualization_4", "*.png"))  
img_path5 = glob.glob(os.path.join(DATA_PATH, "four_visualization_5", "*.png")) 
save_path = f'D:/TEST/image_test/img_hebing'
img_path1.sort()
img_path2.sort()
img_path3.sort()
img_path3.sort()
img_path5.sort()
for idx in range(0, len(img_path3)):
    img_path = img_path3[idx]
    filename = img_path.split('/')[-1].split('\\')[-1].split('.')[0].split('_')[2:-1]
    filename = '_'.join(filename)  # 文件名
    # 读取图像
    img1 = cv2.imread(img_path1[idx])
    img2 = cv2.imread(img_path2[idx])    
    img3 = cv2.imread(img_path3[idx])
    img4 = cv2.imread(img_path4[idx])
    img5 = cv2.imread(img_path5[idx])
    # 获取图像高度和宽度
    height0, width0 = img1.shape[:2]
    height1, width1 = img2.shape[:2]
    height2, width2 = img3.shape[:2]
    height3, width3 = img4.shape[:2]    
    height4, width4 = img5.shape[:2]
    # 获取最宽宽度
    max_width = max(width0, width1, width2, width3, width4)
    # 计算放大倍率
    scaling_factor0 = max_width / width0
    scaling_factor1 = max_width / width1
    scaling_factor2 = max_width / width2
    scaling_factor3 = max_width / width3
    scaling_factor4 = max_width / width4

    # 调整图像大小并进行比例放大
    image0_resized = cv2.resize(img1, (max_width, int(height0 * scaling_factor0)))
    image1_resized = cv2.resize(img2, (max_width, int(height1 * scaling_factor1)))
    image2_resized = cv2.resize(img3, (max_width, int(height2 * scaling_factor2)))
    image3_resized = cv2.resize(img4, (max_width, int(height3 * scaling_factor3)))    
    image4_resized = cv2.resize(img5, (max_width, int(height4 * scaling_factor4)))
    # 拼接
    combined_iamge = np.vstack((image4_resized, image2_resized, image3_resized, image0_resized, image1_resized))
    # 安装路径保存
    cv2.imwrite(os.path.join(save_path, "{}.png".format(filename)), combined_iamge)

拼接好的图像示例如下,当然这是垂直拼接,要是水平拼接,换一下hastack函数,放大倍率换成高度对比就好了


http://www.niftyadmin.cn/n/5269049.html

相关文章

drf入门规范

一 Web应用模式 在开发Web应用中,有两种应用模式: 1.1 前后端不分离 1.2 前后端分离 二 API接口 为了在团队内部形成共识、防止个人习惯差异引起的混乱,我们需要找到一种大家都觉得很好的接口实现规范,而且这种规范能够让后端写…

pycdc配置和使用

配置 使用的kali2023版本 安装cmake pip install cmake 下载pycdc git clone https://github.com/zrax/pycdc 切换到pycdc目录 cd pycdc 进行cmake cmake CMakeLists.txt make 使用 显示帮助信息 ./pycdc -h 显示帮助信息 反编译 ./pycdc /home/kali/Desktop/main…

el-table合并相同数据的单元格

相同的数据合并单元格 <el-table :data"userList" :span-method"objectSpanMethod" border><el-table-column type"selection" width"50" align"center" /><el-table-column label"用户名称" a…

CSS文本样式(详解)

CSS文本样式 &#x1f367; 文本颜色&#x1f9c1;文本缩进&#x1f368;文本对齐&#x1f365;文本行高&#x1f95d;文本装饰 &#x1f367; 文本颜色 属性&#xff1a;color 作用&#xff1a;设置文本颜色 属性值&#xff1a; 颜色表示方式表示含义属性值颜色名称预定义的…

如何处理PHP开发中的单元测试和自动化测试?

如何处理PHP开发中的单元测试和自动化测试&#xff0c;需要具体代码示例 随着软件开发行业的日益发展&#xff0c;单元测试和自动化测试成为了开发者们重视的环节。PHP作为一种广泛应用于Web开发的脚本语言&#xff0c;单元测试和自动化测试同样也在PHP开发中扮演着重要的角色…

giee 添加公匙 流程记录

一、安装 百度网盘CSDN4文件夹下&#xff0c;或者官网下载&#xff1a;https://git-scm.com/downloads 二、生成密钥 1.右击打开git bash 2.$ ssh-keygen -t rsa -C “个人邮箱地址”&#xff0c;按3个回车&#xff0c;密码为空。 3.在C:\Users{windows用户名}.ssh目录下得到…

Java与前端:风云变幻的技术之路

前言 近日&#xff0c;有关“Java已死、前端已凉”的言论在IT圈内流传甚广&#xff0c;引起了广泛关注和讨论。这究竟是真相还是一场对技术人员的焦虑贩卖呢&#xff1f;让我们一同探讨这场技术风暴带来的变化与机遇&#xff0c;并分享一些实用的建议。 一、技术变革的常态 …

GZ015 机器人系统集成应用技术样题3-学生赛

2023年全国职业院校技能大赛 高职组“机器人系统集成应用技术”赛项 竞赛任务书&#xff08;学生赛&#xff09; 样题3 选手须知&#xff1a; 本任务书共 26页&#xff0c;如出现任务书缺页、字迹不清等问题&#xff0c;请及时向裁判示意&#xff0c;并进行任务书的更换。参赛队…