import os
import glob
import shutil
file_path = "H:\\google_download\\nyu_depth_v2\\official_splits\\test"
path_list = os.listdir(file_path)
# 保存路径
save_img = 'H:\\google_download\\nyu_test\\images\\test'
if not os.path.isdir(save_img):
os.makedirs(save_img)
save_label = 'H:\\google_download\\nyu_test\\annotations\\test'
if not os.path.isdir(save_label):
os.makedirs(save_label)
path_name = []
for scene in path_list:
# 读取文件夹中图片
Image_dir = os.path.join(file_path, scene)
img_list = os.listdir(Image_dir)
for img_name in img_list:
if 'jpg' in img_name:
new_img_name = scene + '_' + img_name
old_img_path = Image_dir + '/' + img_name
shutil.copy(old_img_path, save_img + '/' + new_img_name)
if 'png' in img_name:
img_name_split = img_name.split('_')
new_img_name = scene + '_rgb_' + img_name_split[-1]
old_img_path = Image_dir + '/' + img_name
shutil.copy(old_img_path, save_label + '/' + new_img_name)
# with open("dataset.txt", "a") as file:
# for file_name in path_name:
# file.write(file_name + "\n")
# print(file_name)
# file.close()
python遍历文件夹图片,修改文件名并复制图片到另一个文件夹
于 2023-09-15 15:26:34 首次发布