python批量拼接两个文件夹相同名字的图片
生活随笔
收集整理的這篇文章主要介紹了
python批量拼接两个文件夹相同名字的图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?結(jié)果如上圖所示:
為了直觀的觀察圖片效果:可以將圖片拼接在一起:代碼如下
import cv2 import numpy as np import pandas as pd import os, hashlib, shutildef images_concat(left_img_path, right_img_path, concat_image_path, text=None):''':param left_img_path: 左側(cè)圖片本地路徑:param right_img_path: 右側(cè)圖片本地路徑:param concat_image_path: 拼接結(jié)果保存路徑:param text: 批注文字:return: None'''if not left_img_path and not right_img_path:returnw_final = h_final = h_left = w_left = h_right = w_right = 0if left_img_path:left_img = cv2.imread(left_img_path)h_left, w_left = left_img.shape[:2]w_final += w_leftif right_img_path:right_img = cv2.imread(right_img_path)h_right, w_right = right_img.shape[:2]w_final += w_righth_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_imgcanvas[0:h_right, w_left - 1:-1] = right_imgelif left_img_path:canvas[0:h_left, 0:w_left] = left_imgelif right_img_path:canvas[0:h_right, 0:w_right] = right_imgif 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'I:' # 左圖地址 img_b = r'I:' # 右圖地址 img_c = r'I:' # 保存地址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) print(len(a_name)) print('success')總結(jié)
以上是生活随笔為你收集整理的python批量拼接两个文件夹相同名字的图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 糖醋鲤鱼——经典美食
- 下一篇: Cocos2d-html5《王者之剑》实