from PIL import Image def comb(png1, png2, png3,png4,png5,style='horizontal'): img1, img2,img3, img4,img5= Image.open(png1), Image.open(png2) ,Image.open(png3) ,Image.open(png4) ,Image.open(png5) # 统一图片尺寸,可以自定义设置(宽,高) img1 = img1.resize(img1.size, Image.LANCZOS) img2 = img2.resize(img1.size, Image.LANCZOS) img3 = img3.resize(img1.size, Image.LANCZOS) img4 = img4.resize(img1.size, Image.LANCZOS) img5 = img5.resize(img1.size, Image.LANCZOS) size1, size2,size3,size4,size5 = img1.size, img2.size,img3.size, img4.size,img5.size if style == 'horizontal': joint = Image.new('RGB', (size1[0]+size2[0]+size3[0]+size4[0]+size5[0], size1[1])) loc1, loc2 ,loc3,loc4,loc5= (0, 0), (size1[0],0),(size1[0]+size2[0],0),(size1[0]+size2[0]+size3[0],0),(size1[0]+size2[0]+size3[0]+size4[0], 0) joint.paste(img1, loc1) joint.paste(img2, loc2) joint.paste(img3, loc3) joint.paste(img4, loc4) joint.paste(img5, loc5) joint.save('horizontal.jpg') elif style == 'vertical': joint = Image.new('RGB', (size1[0], size1[1] + size2[1]+size3[1]+size4[1]+size5[1]+5)) loc1, loc2 ,loc3,loc4,loc5= (0, 0), (0, size1[1]),(0,size1[1]+size2[1]),(0,size1[1]+size2[1]+size3[1]),(0,+size1[1]+size2[1]+size3[1]+size4[1]) joint.paste(img1, loc1) joint.paste(img2, loc2) joint.paste(img3, loc3) joint.paste(img4, loc4) joint.paste(img5, loc5) joint.save('vertical.jpg') if __name__ == '__main__': # 两张图片地址: png1 = r"./1.png" png2 = r"./2.png" png3 = r"./3.png" png4 = r"./4.png" png5 = r"./5.png" # 左右拼接 comb(png1, png2,png3,png4,png5, style='horizontal') # 上下拼接 #comb(png1, png2,png3,png4,png5,style='vertical')
标签:size1,圖片,python,demo,Image,joint,img1,paste,size From: https://www.cnblogs.com/luoxh-whn/p/18241871