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)
  • 相关阅读:
    Mysql与Sql Server查询数据库中表以及表字段
    linux系统常用命令
    linux tomcat安装以及配置
    mysql 5.7 设置root远程访问
    linux jdk安装
    ubuntu系统阅读CHM文档的最终解决方案
    ubuntu18.04完全卸载mysql的命令
    linux下使用cd命令进入wine容器中的windows路径
    Python中yield关键字的用法及运行逻辑
    Ubuntu18..04.2服务器版设置redis开机启动遇到的问题
  • 原文地址:https://www.cnblogs.com/wjjcjj/p/12307095.html
Copyright © 2011-2022 走看看