python 工具 图片批量合并
注:代码两处设置 region = (4,3,x-3,y-5) 目的是crop剪去图片的白边,这个可以视情况改变
图片需要命名为 x_1.png .....这样的格式
#encoding=utf-8 import numpy as np from PIL import Image import glob if __name__=='__main__': prefix="x" files=glob.glob(prefix+'_*') num=len(files) filename_lens=[len(x) for x in files] #length of the files min_len=min(filename_lens) #minimal length of filenames max_len=max(filename_lens) #maximal length of filenames if min_len==max_len:#the last number of each filename has the same length files=sorted(files) #sort the files in ascending order else:#maybe the filenames are:x_0.png ... x_10.png ... x_100.png index=[0 for x in range(num)] for i in range(num): filename=files[i] start=filename.rfind('_')+1 end=filename.rfind('.') file_no=int(filename[start:end]) index[i]=file_no index=sorted(index) files=[prefix+'_'+str(x)+'.png' for x in index] print(files[0]) baseimg=Image.open(files[0]) x, y=baseimg.size region = (4,3,x-3,y-5) baseimg= baseimg.crop(region) sz=baseimg.size basemat=np.atleast_2d(baseimg) for i in range(1,num): file=files[i] im=Image.open(file) x, y=baseimg.size region = (4,3,x-3,y-5) im= im.crop(region) im=im.resize(sz,Image.ANTIALIAS) mat=np.atleast_2d(im) print(file) basemat=np.append(basemat,mat,axis=1) final_img=Image.fromarray(basemat) final_img.save('merged.bmp')