以下是一个满足您需求的Python图片分类工具脚本:

import os
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("800x600")

        self.image_paths = []
        self.current_index = 0
        self.display_images = []
        self.folder_buttons = []

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

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

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

        self.save_path_entry = tk.Entry(self.save_button_frame, width=50)
        self.save_path_entry.pack(side=tk.LEFT, padx=10)
        self.save_path_entry.insert(0, "保存路径")

        self.save_button = tk.Button(self.save_button_frame, text="保存", command=self.save_images)
        self.save_button.pack(side=tk.LEFT, padx=10)

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

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

        self.load_button = tk.Button(self.button_frame, text="加载文件夹", command=self.load_folder)
        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(4):
            classify_button = tk.Button(self.classify_buttons_frame, text="分类 {}".format(i+1), command=lambda i=i: self.classify_images(i))
            classify_button.pack(side=tk.LEFT, padx=10)
            self.classify_buttons.append(classify_button)

        self.image_label_1 = tk.Label(self.image_frame)
        self.image_label_1.grid(row=0, column=0, padx=10)

        self.image_label_2 = tk.Label(self.image_frame)
        self.image_label_2.grid(row=0, column=1, padx=10)

        self.image_label_3 = tk.Label(self.image_frame)
        self.image_label_3.grid(row=0, column=2, padx=10)

        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)):
                self.display_images_buttons[i].destroy()
            self.display_images_buttons = []
            for i in range(min(len(self.image_paths)-self.current_index, len(self.classify_buttons))):
                self.display_images.append(self.image_paths[self.current_index + i])
                image = Image.open(self.image_paths[self.current_index + i])
                image = image.resize((200, 200))
                photo = ImageTk.PhotoImage(image)
                label = tk.Label(self.image_frame, image=photo)
                label.image = photo
                label.grid(row=0, column=i, padx=10)
                self.display_images_buttons.append(label)

    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)):
                self.display_images_buttons[i].destroy()
            self.display_images_buttons = []
            for i in range(min(len(self.image_paths)-self.current_index, len(self.classify_buttons))):
                self.display_images.append(self.image_paths[self.current_index + i])
                image = Image.open(self.image_paths[self.current_index + i])
                image = image.resize((200, 200))
                photo = ImageTk.PhotoImage(image)
                label = tk.Label(self.image_frame, image=photo)
                label.image = photo
                label.grid(row=0, column=i, padx=10)
                self.display_images_buttons.append(label)

    def classify_images(self, index):
        folder_path = filedialog.askdirectory()
        for image_path in self.display_images:
            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 save_images(self):
        folder_path = self.save_path_entry.get()
        if folder_path != "保存路径":
            for image_path in self.display_images:
                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))

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

请确保已安装tkinterPillow

帮我用python写一个图片分类工具脚本按名称顺序打开文件夹的文件在一个图片框里依次横向显示出该文件夹里的图片可以为自定义同时显示1张可以为2张可以为3张同时在图片框下方自定义四个按钮按钮依次对应文件夹名点击按钮将图片框里显示的所有图片保存到对应的文件夹里文件夹的保存路径以及命名自定义。每分类完图片框里的图片自动切换到下一组图片直到文件夹里的图片全部处理完为止。同时打印出每次处理完的图片名字和图片

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

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