import os import shutil import tkinter as tk 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.num_display_images = 3  # 每行显示的图片数量
    self.num_classify_buttons = 4  # 分类按钮的数量

    self.canvas = tk.Canvas(self.root)
    self.canvas.pack(pady=50)

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

    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)

    self.root.bind('<Configure>', self.update_display)

    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 + self.num_display_images < len(self.image_paths):
        self.current_index += self.num_display_images
        self.load_images()

def prev_images(self):
    if self.current_index - self.num_display_images >= 0:
        self.current_index -= self.num_display_images
        self.load_images()

def load_images(self):
    self.canvas.delete('all')
    for i in range(self.num_display_images):
        if self.current_index + i < len(self.image_paths):
            image_path = self.image_paths[self.current_index + i]
            self.load_image(image_path, i)

def load_image(self, image_path, index):
    image = Image.open(image_path)
    width, height = self.canvas.winfo_width(), self.canvas.winfo_height()
    image.thumbnail((width//self.num_display_images, height))
    photo = ImageTk.PhotoImage(image)
    self.canvas.create_image((index+0.5)*(width//self.num_display_images), height//2, image=photo)
    self.canvas.image = photo

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.image_paths[self.current_index + index]
    image_name = os.path.basename(image_path)
    new_path = os.path.join(folder_path, image_name)
    shutil.move(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 update_display(self, event):
    self.load_images()

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

图片分类工具 - 批量整理图片

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

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