使用 Python 脚本根据图片目录生成 TXT 文件并删除缺失图片

本文介绍如何使用 Python 脚本,根据 G:\image 目录下的文件夹和图片信息,生成一个 TXT 文件,并根据该文件删除 G:\picture 目录下对应文件夹中不存在的图片。

实现思路

  1. 遍历 G:\image 目录下的文件夹,获取文件夹名称和文件夹中的图片名称,将图片名称写入 txt 文件中。

  2. 读取 txt 文件内容,获取每个文件夹中的图片名称列表。

  3. 遍历 G:\picture 目录下的文件夹,获取文件夹名称和文件夹中的图片名称,找到与 txt 文件中对应文件夹名称相同的文件夹,将其中名称不在对应 txt 文件中的图片删除。

具体实现步骤

1. 生成 TXT 文件

import os

image_dir = r'G:\image'
txt_file = r'G:\image\image.txt'

with open(txt_file, 'w') as f:
    for folder_name in os.listdir(image_dir):
        folder_path = os.path.join(image_dir, folder_name)
        if os.path.isdir(folder_path):
            for file_name in os.listdir(folder_path):
                file_path = os.path.join(folder_path, file_name)
                if os.path.isfile(file_path):
                    f.write(folder_name + '/' + file_name + '\n')

2. 读取 TXT 文件

txt_file = r'G:\image\image.txt'

image_dict = {}
with open(txt_file, 'r') as f:
    for line in f:
        line = line.strip()
        folder_name, file_name = line.split('/')
        if folder_name not in image_dict:
            image_dict[folder_name] = []
        image_dict[folder_name].append(file_name)

3. 删除缺失图片

picture_dir = r'G:\picture'

for folder_name in os.listdir(picture_dir):
    folder_path = os.path.join(picture_dir, folder_name)
    if os.path.isdir(folder_path) and folder_name in image_dict:
        for file_name in os.listdir(folder_path):
            file_path = os.path.join(folder_path, file_name)
            if os.path.isfile(file_path) and file_name not in image_dict[folder_name]:
                os.remove(file_path)

完整代码

import os

# 遍历 G:\image 目录下的文件夹,获取文件夹名称和文件夹中的图片名称,将图片名称写入 txt 文件中。
image_dir = r'G:\image'
txt_file = r'G:\image\image.txt'

with open(txt_file, 'w') as f:
    for folder_name in os.listdir(image_dir):
        folder_path = os.path.join(image_dir, folder_name)
        if os.path.isdir(folder_path):
            for file_name in os.listdir(folder_path):
                file_path = os.path.join(folder_path, file_name)
                if os.path.isfile(file_path):
                    f.write(folder_name + '/' + file_name + '\n')

# 读取 txt 文件内容,获取每个文件夹中的图片名称列表。
txt_file = r'G:\image\image.txt'

image_dict = {}
with open(txt_file, 'r') as f:
    for line in f:
        line = line.strip()
        folder_name, file_name = line.split('/')
        if folder_name not in image_dict:
            image_dict[folder_name] = []
        image_dict[folder_name].append(file_name)

# 遍历 G:\picture 目录下的文件夹,获取文件夹名称和文件夹中的图片名称,找到与 txt 文件中对应文件夹名称相同的文件夹,将其中名称不在对应 txt 文件中的图片删除。
picture_dir = r'G:\picture'

for folder_name in os.listdir(picture_dir):
    folder_path = os.path.join(picture_dir, folder_name)
    if os.path.isdir(folder_path) and folder_name in image_dict:
        for file_name in os.listdir(folder_path):
            file_path = os.path.join(folder_path, file_name)
            if os.path.isfile(file_path) and file_name not in image_dict[folder_name]:
                os.remove(file_path)
Python 脚本:根据图片目录生成 TXT 文件并删除缺失图片

原文地址: https://www.cveoy.top/t/topic/oSM1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录