python+图像对的合并和分离
1、图像对的合并,即将trainA0中图像1_aa.jpg,trainB0中图像0_aa.jpg,合并到all中,图像对按着,即str(num)_1_aa.jpg,str(num)_0_aa.jpg
import os import shutil Adir = './trainA0/' Bdir = './trainB0/' alldir = './all/' if not os.path.exists(Adir): print ("The source path trainA0 is not exist") if not os.path.exists(Bdir): print ("The source path trainB0 is not exist") if not os.path.exists(alldir): os.mkdir(alldir) num=0 for fi in os.listdir(Adir): im_name = '0' + fi[1:] shutil.copyfile(Adir+fi,alldir+str(num)+'_'+fi) #把文件夹Adir中的文件fi,复制到文件夹alldir中,并重新命名为str(num)+'_'+fi shutil.copyfile(Bdir+im_name,alldir+str(num)+'_'+im_name) num = num + 1 print (num)
2、图像对的分离,all中,图像对按着,即str(num)_1_aa.jpg,str(num)_0_aa.jpg,分离到trainA0中图像1_aa.jpg,trainB0中图像0_aa.jpg
import os import shutil alldir = './all/' Adir = './A/' Bdir = './B/' if not os.path.exists(alldir): print ("The source path alldir is not exist") if not os.path.exists(Adir): os.mkdir(Adir) if not os.path.exists(Bdir): os.mkdir(Bdir) num=0 for fi in os.listdir(alldir): num_str = fi.split('_')[0] im_name = fi[len(num_str)+1:] if fi.split('_')[1]=='1': shutil.copyfile(alldir+fi,Adir+im_name) else: shutil.copyfile(alldir+fi,Bdir+im_name) num = num + 1 print (num)
3、图像对的挑选,A中图像1_aa.jpg,B中图像0_aa.jpg,B中图像被混合到了trainB0中,现在从trainB0中挑选出正确的图像到B中。
import os import shutil Adir = './A/' Bdir = './trainB0/' alldir = './B/' if not os.path.exists(Adir): print ("The source path trainA0 is not exist") if not os.path.exists(Bdir): print ("The source path trainB0 is not exist") if not os.path.exists(alldir): os.mkdir(alldir) num=0 for fi in os.listdir(Adir): im_name = '0' + fi[1:] shutil.copyfile(Bdir+im_name,alldir+im_name) num = num + 1 print (num)