import sys
sys.path.append("/data")
import numpy as np
import cv2
import os
import os.path as osp
import shutil
from scipy.io import loadmat
def bins():
index = 0
# index_B = 0
# index_F = 0
# index_L = 0
# index_R = 0
all_path = "/data/SRC_Image_no_dir"
chosen_path = "/data/img_bird/"
save_path = "/data/images/"
os.makedirs(save_path, exist_ok=True)
#search images from the chosen path
for root_chosen_path, dirs, files in os.walk(chosen_path):
# print("root_chosen_path:", root_chosen_path)
# print("dirs:", dirs)
# print("files:", files)
for chosen_name in files:
#print("chosen_name###################################:", chosen_name)
#if chosen_name.endswith("png"):
#print("chosen_name###################################:", chosen_name)
chosen_img_path = osp.join(root_chosen_path, chosen_name)
print("chosen_img_path:", chosen_img_path)
save_chosen_path = os.path.split(chosen_img_path)[0].replace("img_bird", "images")
os.makedirs(save_chosen_path, exist_ok=True)
#print("save_chosen_path:", save_chosen_path)
#dir_name = osp.split(chosen_name)[1].split(".")[0][:-5]
img_name_prefix = chosen_name.split(".")[0]
# print("img_name_prefix:", img_name_prefix)
# label_name = osp.split(chosen_name)[1].split("_")[5]
# label_name = label_name.replace("png", "jpg")
#print("label_name:", label_name)
#find the 4-way iamges with the same name of the current image from all path (tree-structured directory)
# for root_all_path, dirs_, files_ in os.walk(all_path):
# #print("root_all_path:", root_all_path)
# root_all_path_name = osp.split(root_all_path)[1].split("/")[-1]
# print("root_all_path_name:", root_all_path_name)
# if root_all_path_name == dir_name:
# #print("root_all_path:", root_all_path)
# #print("dir_name:", dir_name)
# B_dir = osp.join(root_all_path, "B/")
# #print("B_dir", B_dir)
# F_dir = osp.join(root_all_path, "F/")
# R_dir = osp.join(root_all_path, "R/")
# L_dir = osp.join(root_all_path, "L/")
# for picture in os.listdir(B_dir):
# #print("picture:", picture)
# if picture == label_name:
# #print("picture:", picture)
# label_number = osp.split(label_name)[1].split(".")[0]
# #print("label_number:", label_number)
# B_save_path = osp.join(save_chosen_path, dir_name + "_" + label_number + "_B.jpg")
# #print("B_save_path:", B_save_path)
# shutil.copy(os.path.join(B_dir, picture), B_save_path)
# index_B = index_B + 1
# for picture in os.listdir(F_dir):
# if picture == label_name:
# label_number = osp.split(label_name)[1].split(".")[0]
# F_save_path = osp.join(save_chosen_path, dir_name + "_" + label_number + "_F.jpg")
# # print("F_save_path:", F_save_path)
# shutil.copy(os.path.join(F_dir, picture), F_save_path)
# index_F = index_F + 1
# for picture in os.listdir(R_dir):
# if picture == label_name:
# label_number = osp.split(label_name)[1].split(".")[0]
# R_save_path = osp.join(save_chosen_path, dir_name + "_" + label_number + "_R.jpg")
# # print("R_save_path:", R_save_path)
# shutil.copy(os.path.join(R_dir, picture), R_save_path)
# index_R = index_R + 1
# for picture in os.listdir(L_dir):
# if picture == label_name:
# label_number = osp.split(label_name)[1].split(".")[0]
# L_save_path = osp.join(save_chosen_path, dir_name + "_" + label_number + "_L.jpg")
# # print("L_save_path:", L_save_path)
# shutil.copy(os.path.join(L_dir, picture), L_save_path)
# index_L = index_L + 1
# index = index_B + index_F + index_L + index_R
# print("done, copy %s images in total" % (index))
###original image in one folder
for root_all_path, dirs_, files_ in os.walk(all_path):
# print("root_all_path:", root_all_path)
# print("dirs_:", dirs_)
# print("files_:", files_)
for all_img_name in files_:
files_prefix = all_img_name.split(".")[0][:-2]
files_suffix_direction = "_" +all_img_name.split("_",6)[6].split(".")[0]
if img_name_prefix == files_prefix:
# print("all_img_name:", all_img_name)
# print("img_name_prefix:", img_name_prefix)
# print("files_suffix_direction:", files_suffix_direction)
save_path = osp.join(save_chosen_path, img_name_prefix + files_suffix_direction + ".jpg")
print("save_path:", save_path)
shutil.copy(os.path.join(all_path, all_img_name), save_path)
index = index + 1
print("done, copy %s images in total" % (index))
if __name__ == '__main__':
bins()
select_BFRL
最新推荐文章于 2024-09-05 11:10:46 发布
该博客介绍了如何使用Python脚本从'img_bird'目录中选择图像,并在'src_Image_no_dir'路径下找到与其名称相同的四个方向(B、F、L、R)的图像,最终将所有相关图像复制到'images'子目录。
摘要由CSDN通过智能技术生成