zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    Pytorch:常用工具模块
    Pytorch: torch.nn
    流行框架阶段·概览
    jquery/js实现一个网页同时调用多个倒计时(最新的)
    用js判断页面刷新或关闭的方法
    js中substr,substring,indexOf,lastIndexOf,split 的用法
    jsonp案例
    CSS布局奇技淫巧:各种居中
    IntelliJ Idea 常用快捷键列表
    jquery中attr和prop的区别
  • 原文地址:https://www.cnblogs.com/wjjcjj/p/12307095.html
Copyright © 2011-2022 走看看