Python 把图片横向/纵向拼接

 

两张图片拼接

import numpy as np
from PIL import Image


img = Image.open('test.jpg') # 打开图片
im = np.array(img) # 转化为ndarray对象

im1 = np.concatenate((im, im), axis = 0) # 纵向拼接
im2 = np.concatenate((im, im), axis = 1) # 横向拼接

# 生成图片
img1 = Image.fromarray(im1)
img2 = Image.fromarray(im2)

# 保存图片
img1.save('test1.jpg')
img2.save('test2.jpg')

多张图片拼接

import numpy as np

from PIL import Image

paths = ['./1.jpg', './2.jpg', './3.jpg', './4.jpg']
img_array = ''
img = ''
for i, v in enumerate(paths):
    if i == 0:
        img = Image.open(v)  # 打开图片
        img_array = np.array(img)  # 转化为np array对象
    if i > 0:
        img_array2 = np.array(Image.open(v))
        img_array = np.concatenate((img_array, img_array2), axis=1)  # 横向拼接
        # img_array = np.concatenate((img_array, img_array2), axis=0)  # 纵向拼接
        img = Image.fromarray(img_array)



# 保存图片
img.save('test.jpg')

OK.

 

你可能感兴趣的:(python,python,opencv)