接上回继续转换

# # import os
# # import re
# # from collections import Counter
# #
# # # 设置输入和输出文件夹
# # input_folder = r'E:\oneoneone\oneoneone\one\project-11-at-2024-07-29-10-34-c071633f'
# # output_folder = r'E:\oneoneone\oneoneone\one\output'
# #
# # # 创建输出文件夹
# # if not os.path.exists(output_folder):
# #     os.makedirs(output_folder)
# # # 遍历输入文件夹中的文件
# # for filename in os.listdir(input_folder):
# #     if filename.endswith('.png'):
# #         # 提取 task-和-annotation 之间的数字
# #         match = re.search(r'task-(\d+)-annotation', filename)
# #         if match:
# #             number = int(match.group(1))
# #
# #             # 统计该数字出现的次数
# #             counter = Counter()
# #             for existing_file in os.listdir(output_folder):
# #                 if existing_file.startswith(f'{number}'):
# #                     counter[number] += 1
# #
# #             # 构建新的文件名
# #             if counter[number] == 0:
# #                 new_filename = f'{number}.png'
# #             else:
# #                 new_filename = f'{number}-{counter[number] + 1}.png'
# #
# #             # 将文件移动到输出文件夹
# #             src_path = os.path.join(input_folder, filename)
# #             dst_path = os.path.join(output_folder, new_filename)
# #             os.rename(src_path, dst_path)
# #             print(f'Renamed {filename} to {new_filename}')
# #         else:
# #             # 没有找到匹配的数字,跳过该文件
# #             print(f'Skipping {filename}')
#
# import os
# import json
# import shutil
#
# # 设置输入 JSON 文件路径和输入图像文件夹路径
# json_file_path = 'E:/oneoneone/oneoneone/one/annotations/ann.json'
# output_folder = r'E:\oneoneone\oneoneone\one\test'
# input_folder = r'E:\oneoneone\oneoneone\one\images'
#
# # 创建输出文件夹(如果不存在)
# if not os.path.exists(output_folder):
#     os.makedirs(output_folder)
#
# # 读取 JSON 文件
# try:
#     with open(json_file_path, 'r') as json_file:
#         data = json.load(json_file)
# except json.JSONDecodeError as e:
#     print(f"Error decoding JSON file: {e}")
#     exit()
#
# # 检查 JSON 数据中是否存在 "image" 键
# if 'images' in data:
#     images_data = data['images']
#     for image_dict in images_data:
#
#         # 获取 "id" 和 "filename" 的值
#         image_id = image_dict.get('id', None)
#         print("image_id",image_id)
#         image_filename = image_dict.get('file_name', None)
#         print("filename",image_filename)
#
#         # 如果 "id" 和 "filename" 都存在,则复制图像文件
#         if image_id is not None and image_filename is not None:
#             # 构建输入和输出文件路径
#             file_path = os.path.splitext(image_filename)[0]
#             src_file_path = os.path.join(input_folder, f'{file_path}.JPG')
#             print(src_file_path)
#             dst_file_path = os.path.join(output_folder, f'{image_id}.JPG')
#
#             # 复制图像文件
#             try:
#                 shutil.copyfile(src_file_path, dst_file_path)
#                 print(f"Saved image: {image_filename}")
#             except FileNotFoundError:
#                 print(f"Error: {image_id}.png not found in the input folder.")
#         else:
#             print("Error: 'id' or 'filename' not found in the image dictionary.")
# else:
#     print("Error: 'image' key not found in the JSON file.")


import os
import shutil
from pathlib import Path

# 设置两个文件夹的路径
folder1_path = r'E:\oneoneone\oneoneone\one\test1'
folder2_path = r'E:\oneoneone\oneoneone\one\output'

import os
import shutil
from pathlib import Path

# 设置两个文件夹的路径
folder1_path = r'E:\oneoneone\oneoneone\one\test1'
folder2_path = r'E:\oneoneone\oneoneone\one\output'

for filename1 in os.listdir(folder1_path):
    base1, ext1 = os.path.splitext(filename1)
    if '_' in base1:
        # 删除带有_的图像
        original_file_path = os.path.join(folder1_path, filename1)
        os.remove(original_file_path)
        print(f"Deleted {original_file_path}")
        continue

    count = 0

    for filename2 in os.listdir(folder2_path):
        base2, ext2 = os.path.splitext(filename2)

        # 如果文件夹1中的文件前缀与文件夹2中的文件前缀匹配
        if base1 in base2:
            count += 1

            # 构建新文件名并复制文件
            new_filename = f"{base2}{ext1}"
            new_file_path = os.path.join(folder1_path, new_filename)
            original_file_path = os.path.join(folder1_path, filename1)

            if not os.path.exists(new_file_path):
                shutil.copy(original_file_path, new_file_path)
                print(f"Copied {original_file_path} to {new_file_path}")

    # 如果没有找到匹配的文件,则复制原文件
    if count == 0:
        new_file_path = os.path.join(folder1_path, f"{base1}{ext1}")
        original_file_path = os.path.join(folder1_path, filename1)
        shutil.copy(original_file_path, new_file_path)
        print(f"Copied {original_file_path} to {new_file_path}")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值