拼接不同文件夹中同名图片的方法
2023-12-13 17:22:03
有时候为了方便对比不同文件夹中同名图片,需要拼接在一起,这里提供一个拼接方法,当然不同命文件也可以实现拼接,稍微改改就能实现
如下图,在文件夹中有五个文件夹中的图片需要拼接,拼接后的图片存放在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函数,放大倍率换成高度对比就好了
文章来源:https://blog.csdn.net/m0_73832962/article/details/134972735
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!