Python 代码:将 TXT 文件中的图片路径和标签移动到同一文件夹
以下是一个完整的 Python 代码示例,用于从 TXT 文件中读取图片路径和标签,然后将图片和对应的标签放置到同一个文件夹中。
import shutil
def move_images(txt_file, destination_folder):
with open(txt_file, 'r') as file:
lines = file.readlines()
for line in lines:
path, label = line.strip().split(' ') # 假设路径和标签之间使用空格分隔
shutil.copy2(path, destination_folder) # 将图片复制到目标文件夹
shutil.copy2(label, destination_folder) # 将标签复制到目标文件夹
# 示例用法
txt_file = 'image_list.txt'
destination_folder = 'output_folder'
move_images(txt_file, destination_folder)
请注意,以上代码假设图片路径和标签之间使用空格进行分隔,并使用 shutil.copy2 方法将图片和标签复制到目标文件夹。如果你的 TXT 文件格式不同或者你有其他特定的需求,你可以根据实际情况进行相应的修改。
原文地址: https://www.cveoy.top/t/topic/phWp 著作权归作者所有。请勿转载和采集!