python批量拼接两个文件夹相同名字的图片

import cv2
import numpy as np
import pandas as pd
import os,hashlib,shutil

def images_concat(left_img_path, right_img_path, concat_image_path, text=None):
    '''
    :param left_img_path: 左侧图片本地路径
    :param right_img_path: 右侧图片本地路径
    :param concat_image_path: 拼接结果保存路径
    :param text: 批注文字
    :return: None
    '''
    if not left_img_path and not right_img_path:
        return
    w_final = h_final = h_left = w_left = h_right = w_right = 0
    if left_img_path:
        left_img = cv2.imread(left_img_path)
        h_left, w_left = left_img.shape[:2]
        w_final += w_left
    if right_img_path:
        right_img = cv2.imread(right_img_path)
        h_right, w_right = right_img.shape[:2]
        w_final += w_right
    h_final = max(h_left, h_right) # 以较大的图的高度为准
    if text: h_final += 20 # 为批注文字预留位置

    canvas = np.zeros([h_final, w_final, 3], np.uint8)
    # 填入像素,兼容图片为空的情况
    if left_img_path and right_img_path:
        canvas[0:h_left, 0:w_left] = left_img
        canvas[0:h_right, w_left-1:-1] = right_img
    elif left_img_path:
        canvas[0:h_left, 0:w_left] = left_img
    elif right_img_path:
        canvas[0:h_right, 0:w_right] = right_img

    if text: # 写入文字
        cv2.putText(canvas, text, (5, h_final-5), cv2.FONT_HERSHEY_PLAIN, 15, (0, 0, 255), 25)
    cv2.imwrite(concat_image_path, canvas)


img_a = r'U:\datas\test\t'#左图地址
img_b = r'U:\datas\test\1'#右图地址
img_c = r'U:\datas\test\c\testjyz401'#保存地址

a_name = os.listdir(img_a)
# print(a_name)
b_name = os.listdir(img_b)

for a in a_name:
	print(a)
	for b in b_name:
		if a == b:
			images_concat(img_a+'/'+a, img_b+'/'+b, img_c+'/'+a, text='true')
print(len(a_name))
print('success')

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕容洛凝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值