将需要拼接的图片路径写在对应的img_path,比如有6张,通过hstack和vstack函数将图片拼接在一起。resize尺寸可以根据具体图片大小更改,拼接后的图片效果如下:
图片1 | 图片2 |
---|---|
图片3 | 图片4 |
图片5 | 图片6 |
代码如下:
import cv2
import numpy as np
img1_path = r"C:\Users\user\Desktop\picture\old\07724.jpg"
img2_path = r"C:\Users\user\Desktop\picture\new\07724.jpg"
img3_path = r"C:\Users\user\Desktop\picture\old\07371.jpg"
img4_path = r"C:\Users\user\Desktop\picture\new\07371.jpg"
img5_path = r"C:\Users\user\Desktop\picture\old\01514.jpg"
img6_path = r"C:\Users\user\Desktop\picture\new\01514.jpg"
img1 = cv2.imread(img1_path)
# print(np.shape(img1)) # 或者用img1.shape (h,w,c) (1080,1920,3)
img1 = cv2.resize(img1,(1920,1080))
img2 = cv2.imread(img2_path)
img2 = cv2.resize(img2,(1920,1080))
img3 = cv2.imread(img3_path)
img3 = cv2.resize(img3,(1920,1080)) # resize(img,(w,h),interpolation=cv2.INTER_CUBIC)
img4 = cv2.imread(img4_path)
img4 = cv2.resize(img4,(1920,1080))
img5 = cv2.imread(img5_path)
img5 = cv2.resize(img5,(1920,1080))
img6 = cv2.imread(img6_path)
img6 = cv2.resize(img6,(1920,1080))
img_tmp1 = np.hstack((img1,img2))
print(np.shape(img_tmp1))
img_tmp2 = np.hstack((img3,img4))
print(np.shape(img_tmp2))
img_tmp3 = np.hstack((img5,img6))
print(np.shape(img_tmp3))
img_tmp4 = np.vstack((img_tmp1,img_tmp2))
img = np.vstack((img_tmp4,img_tmp3))
cv2.imwrite(r"C:\Users\user\Desktop\picture\stack.jpg",img)
注意cv2.read读取图片路径不能含有中文,不然会报错。