Python 使用PIL numpy 实现拼接图片的示例

python纵向合并任意多个图片,files是要拼接的文件list

# -*- coding:utf-8 -*-
def mergeReport(files):
 from PIL import Image
 import numpy as np
 baseimg=Image.open(files[0])
 sz = baseimg.size
 basemat=np.atleast_2d(baseimg)
 for file in files[1:]:
  im=Image.open(file)
 #resize to same width
  sz2 = im.size
  if sz2!=sz:
   im=im.resize((sz[0],round(sz2[0] / sz[0] * sz2[1])),Image.ANTIALIAS)
  mat=np.atleast_2d(im)
  basemat=np.append(basemat,mat,axis=0)
 report_img=Image.fromarray(basemat)
 report_img.save('merge.png')