使用库:Pillow代码比较简单,图片水平居中,背景色为白色importosfromPILimportImageif__name__=='__main__':im_list=[]path="图片所在路径"forfninos.listdir(path):iffn.endswith('.jpg'):im_list.append(Image.open(path+os.sep+fn))width=0height=0forimginim_list:#单张图片大小w,h=img.sizeheight+=h#取最大宽度作为拼接图片的宽度width=max(width,w)#创建空白longimageresult=Image.new(im_list[0].mode,(width,height),0xffffff)#拼接图像height=0forimginim_list:w,h=img.size#Imagehorizo??ntallycenteredresult.paste(img,box=(round(width/2-w/2),height))height+=h#保存图片result.save('拼接长图.jpg')
