数据清洗脚本

主要功能是创建一个基于Tkinter的图像浏览器程序,允许用户在指定的根目录下浏览图像文件夹中的图像文件,并具有以下功能:

  1. 图像浏览:用户可以使用左箭头和右箭头键来切换查看下一张和上一张图像。

  2. 图像标记:用户可以按空格键来标记当前查看的图像,将其文件路径添加到已标记的图像列表中,并将标记信息保存到"plot.txt"文件中。

  3. 保存浏览状态:程序会在窗口关闭时保存当前浏览状态,包括当前所在文件夹和图像索引,以便下次重新打开程序时能够恢复到上次浏览的状态,并将标记信息保存到"last_viewed.txt"文件中。

  4. 显示图像信息:程序会在图像上方显示当前图像的文件路径和图像索引信息。

  5. 自动排序:程序会自动将图像文件夹中的图像按数字顺序进行排序。

import os
import re
import tkinter as tk
from PIL import Image, ImageTk, ImageDraw
from tkinter import PhotoImage
from itertools import chain


def numerical_sort(value):
    numbers = re.compile(r'(\d+)')
    parts = numbers.split(value)
    parts[1::2] = map(int, parts[1::2])
    return parts

class ImageBrowser:
    def __init__(self, root_dir):
        self.root_dir = root_dir
        self.image_folders = self._gather_image_folders()
        self.current_folder_index, self.current_image_index = self._load_last_viewed()
        self.marked_images = []

        self.window = tk.Tk()
        self.window.title("Image Browser")
        self.image_label = tk.Label(self.window)
        self.image_label.pack()

        self.window.bind("<Left>", self.prev_image)
        self.window.bind("<Right>", self.next_image)
        self.window.bind("<space>", self.mark_image)
        self.window.protocol("WM_DELETE_WINDOW", self._on_close)

        self.update_image()

    def _gather_image_folders(self):
        image_folders = []
        for subdir, dirs, files in os.walk(self.root_dir):
            if os.path.basename(subdir) == 'plot':
                sorted_files = sorted([file for file in files if file.endswith('.jpg')], key=numerical_sort)
                image_paths = [os.path.join(subdir, file) for file in sorted_files]
                if image_paths:
                    image_folders.append(image_paths)
        return image_folders

    def _load_last_viewed(self):
        try:
            with open("last_viewed.txt", "r") as file:
                folder_index, image_index = map(int, file.readline().split(','))
                if folder_index < len(self.image_folders) and image_index < len(self.image_folders[folder_index]):
                    return folder_index, image_index
        except Exception:
            pass
        return 0, 0

    def _save_last_viewed(self):
        with open("last_viewed.txt", "w") as file:
            file.write(f"{self.current_folder_index},{self.current_image_index}")

    def _on_close(self):
        self._save_last_viewed()
        self.window.destroy()

    def update_image(self):
        if self.image_folders:
            current_folder = self.image_folders[self.current_folder_index]
            image_path = current_folder[self.current_image_index]
            img = Image.open(image_path)
            img.thumbnail((800, 600), Image.ANTIALIAS)

            draw = ImageDraw.Draw(img)
            draw.text((10, 10), image_path, fill="red")
            draw.text((img.width - 70, 10), f"{self.current_image_index + 1}/{len(current_folder)}", fill="red")

            photo = ImageTk.PhotoImage(img)
            self.image_label.config(image=photo)
            self.image_label.image = photo

    def next_image(self, event=None):
        current_folder = self.image_folders[self.current_folder_index]
        if self.current_image_index < len(current_folder) - 1:
            self.current_image_index += 1
        else:
            if self.current_folder_index < len(self.image_folders) - 1:
                self.current_folder_index += 1
                self.current_image_index = 0
        self.update_image()

    def prev_image(self, event=None):
        if self.current_image_index > 0:
            self.current_image_index -= 1
        else:
            if self.current_folder_index > 0:
                self.current_folder_index -= 1
                current_folder = self.image_folders[self.current_folder_index]
                self.current_image_index = len(current_folder) - 1
        self.update_image()

    def mark_image(self, event=None):
        current_folder = self.image_folders[self.current_folder_index]
        marked_image_path = current_folder[self.current_image_index]
        if marked_image_path not in self.marked_images:
            self.marked_images.append(marked_image_path)
            with open("plot.txt", "a") as file:
                file.write(marked_image_path + "\n")

    def run(self):
        self.window.mainloop()

# 写入输入路径
image_browser = ImageBrowser("")
image_browser.run()

   根据plot.txt内容,可以删除不合格图片

import os

def delete_files_from_list(file_list_path):
    try:
        with open(file_list_path, 'r') as file:
            for line in file:
                file_path = line.strip()
                if os.path.isfile(file_path):
                    os.remove(file_path)
                    print(f"Deleted: {file_path}")
                else:
                    print(f"File not found: {file_path}")
    except FileNotFoundError:
        print(f"The file {file_list_path} does not exist.")

delete_files_from_list("plot.txt")

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没了海绵宝宝的派大星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值