例1.jpg和2.jpg是两张(640*480)大小的图片
方法一:利用numpy及cv2实现合并
# -*- coding: utf-8 -*-
#! python3
import cv2
import numpy as np
#原图
img1 = cv2.imread('1.jpg')
img2 = cv2.imread('2.jpg')
#灰色图
#gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
#gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
#====使用numpy的数组矩阵合并concatenate======
# 纵向连接
image = np.vstack((gray1, gray2))
# 横向连接
image = np.concatenate([img1, img2], axis=1)
#=============
cv2.imwrite('new_image.jpg',image)
#cv2.imshow('image', image)
#cv2.waitKey(1)
方法二:利用PIL和matplotlib实现合并
from PIL import Image
import matplotlib.pyplot as plt
img1 = Image.open("./1.jpg")
img2 = Image.open("./2.jpg")
result = Image.new(img1.mode, (640*2, 480 ))
result.paste(img1, box=(0, 0))
result.paste(img2, box=(640, 0))
result.save("./new_image.jpg")
plt.imshow(result)
plt.show()
查看所在目录,new_image.jpg即为合并后的新图片