后面有行数和列数的控制,可以根据需要进行修改。
#!/usr/bin/python3 #encoding=utf-8 import numpy as np from PIL import Image import glob,os if __name__=='__main__': files=glob.glob('100faces/*.png') 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 pass 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]) sz=baseimg.size final_mat = [] for i in range(0, 4): basemat=[] for j in range(6): file=files[i * 6 + j] im=Image.open(file) im=im.resize(sz,Image.ANTIALIAS) mat=np.atleast_2d(im) print(file) if len(basemat) == 0: basemat=np.atleast_2d(mat) else: basemat=np.append(basemat,mat,axis=1) if len(final_mat) == 0: final_mat=np.atleast_2d(basemat) else: final_mat=np.append(final_mat, basemat, axis=0) final_img=Image.fromarray(final_mat) final_img.save('merged_mix.png')