import os import shutil import tkinter as tk import matplotlib.pyplot as plt from tkinter import filedialog from PIL import Image, ImageTk

class ImageClassifier: def init(self, root): self.root = root self.root.title("图片分类工具") self.root.geometry("800x500") self.root.bind("", self.key_press_event)

    self.image_paths = []
    self.current_index = 0
    self.display_images = []
    self.display_images_buttons = []
    self.num_display_images = 3  # Number of images to display in each row
    self.num_classify_buttons = 4  # Number of classify buttons

    self.image_frame = tk.Frame(self.root)
    self.image_frame.pack(pady=50)

    self.button_frame = tk.Frame(self.root)
    self.button_frame.pack(pady=10)

    self.save_buttons_frame = tk.Frame(self.root)
    self.save_buttons_frame.pack(pady=10)

    self.save_paths = []
    self.save_path_entries = []
    self.save_buttons = []

    self.next_button = tk.Button(self.button_frame, text="下一组", command=self.next_images, width=10, height=2)
    self.next_button.pack(side=tk.RIGHT, padx=10)

    self.prev_button = tk.Button(self.button_frame, text="上一组", command=self.prev_images, width=10, height=2)
    self.prev_button.pack(side=tk.RIGHT, padx=10)

    self.load_button = tk.Button(self.button_frame, text="打开图片", command=self.load_folder, width=10, height=2)
    self.load_button.pack(side=tk.LEFT, padx=10)

    self.classify_buttons_frame = tk.Frame(self.root)
    self.classify_buttons_frame.pack(pady=10)

    self.classify_buttons = []
    for i in range(self.num_classify_buttons):
        classify_button = tk.Button(self.classify_buttons_frame, text="类别 {}".format(i+1), command=lambda i=i: self.classify_images(i), width=10, height=2)
        classify_button.pack(side=tk.LEFT, padx=10)
        self.classify_buttons.append(classify_button)

    for i in range(self.num_display_images):
        self.display_images_buttons.append([])

    self.root.mainloop()

def load_folder(self):
    folder_path = filedialog.askdirectory()
    self.image_paths = []
    for file_name in sorted(os.listdir(folder_path)):
        if file_name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):
            self.image_paths.append(os.path.join(folder_path, file_name))
    self.current_index = 0
    self.next_images()

def next_images(self):
    if self.current_index + len(self.display_images) < len(self.image_paths):
        self.current_index += len(self.display_images)
        self.display_images = []
        for i in range(len(self.display_images_buttons)):
            for button in self.display_images_buttons[i]:
                button.destroy()
            self.display_images_buttons[i] = []
        for i in range(min(len(self.image_paths) - self.current_index, self.num_display_images)):
            self.display_images.append(self.image_paths[self.current_index + i])
            image = Image.open(self.image_paths[self.current_index + i])
            # Display the image using PIL
            image = image.resize(image.size)
            photo = ImageTk.PhotoImage(image)
            button = tk.Button(self.image_frame, image=photo, command=lambda i=i: self.classify_images(i))
            button.photo = photo
            button.pack(side=tk.LEFT, padx=10)
            self.display_images_buttons[i].append(button)

def prev_images(self):
    if self.current_index - len(self.display_images) >= 0:
        self.current_index -= len(self.display_images)
        self.display_images = []
        for i in range(len(self.display_images_buttons)):
            for button in self.display_images_buttons[i]:
                button.destroy()
            self.display_images_buttons[i] = []
        for i in range(min(len(self.image_paths) - self.current_index, self.num_display_images)):
            self.display_images.append(self.image_paths[self.current_index + i])
            image = Image.open(self.image_paths[self.current_index + i])
            # Display the image using PIL
            image = image.resize(image.size)
            photo = ImageTk.PhotoImage(image)
            button = tk.Button(self.image_frame, image=photo, command=lambda i=i: self.classify_images(i))
            button.photo = photo
            button.pack(side=tk.LEFT, padx=10)
            self.display_images_buttons[i].append(button)

def classify_images(self, index):
    button = self.classify_buttons[index]
    if hasattr(button, 'save_path'):
        folder_path = button.save_path
    else:
        folder_path = filedialog.askdirectory()
        button.save_path = folder_path

    image_path = self.display_images[index]
    image_name = os.path.basename(image_path)
    new_path = os.path.join(folder_path, image_name)
    shutil.copyfile(image_path, new_path)
    print("图片 {} 移动到了 {}".format(image_name, new_path))

def key_press_event(self, event):
    if event.char.lower() == 'a':
        self.prev_images()
    elif event.char.lower() == 'd':
        self.next_images()

def save_images(self, index):
    folder_path = filedialog.askdirectory()
    self.save_paths[index] = folder_path
    self.save_path_entries[index].delete(0, tk.END)
    self.save_path_entries[index].insert(0, folder_path)

root = tk.Tk() app = ImageClassifier(root

import osimport shutilimport tkinter as tkimport matplotlibpyplot as pltfrom tkinter import filedialogfrom PIL import Image ImageTkclass ImageClassifier def __init__self root selfroot = root

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

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