# srcfile 需要复制、移动的文件
# dstpath 目的地址
# 代码实现功能,根据txt文件名提取并提取出对应文件名的图片
# 根据images图片名提取并提出对应的txt文件名
import os
import shutil
from glob import glob
def mycopyfile(srcfile, dstpath): # 复制函数
if not os.path.isfile(srcfile):
print("%s not exist!" % (srcfile))
else:
fpath, fname = os.path.split(srcfile) # 分离文件名和路径
if not os.path.exists(dstpath):
os.makedirs(dstpath) # 创建路径
shutil.copy(srcfile, dstpath + fname) # 复制文件
print("copy %s -> %s" % (srcfile, dstpath + fname))
src_dir = r'C:\Users\yewenjing\Desktop\简化标签\all_labels/' # 要提取对应图片的文件夹路径 or txt提取
dst_dir = r'C:\Users\yewenjing\Desktop\1023train\upgrade_images_train/' # 目的路径记得加斜杠 保存的地址
txt_path = r'C:\Users\yewenjing\Desktop\1023train\upgrade_images_train' # txt文件名提取 or jpg图片提取
name=[]
# with open(txt_path) as f1:
txt_names = os.listdir(txt_path)
for i in range(len(txt_names)):
# per_txt_path=os.path.join(txt_path,txt_names[i].split(".txt")[0]+".txt")
# name.append(txt_names[i].split(".txt")[0]) # 提取txt文件名
name.append(txt_names[i].split(".jpg")[0]) # 提取jpg文件名
src_file_list=[]
for imges_all in name:
# src_file_list.append(src_dir+imges_all+".jpg") #提取图片对应的jpg文件
src_file_list.append(src_dir+imges_all+".txt") #提取图片对应的txt文件
# print(src_file_list)
for srcfile in src_file_list:
mycopyfile(srcfile, dst_dir) # 复制文件
1.代码实现功能,根据txt文件名并批量提取出对应文件名中的图片 2.根据image的名称批量提取对应文件的txt文件名
最新推荐文章于 2023-05-12 11:48:57 发布