以下是使用 Python 和 tkinter 库开发的图片分类工具,可以方便地加载文件夹中的图片,并根据分类标签将图片移动到不同的文件夹。

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('800x600')

        self.image_paths = []
        self.current_index = 0
        self.display_images = []
        self.display_images_buttons = []  
        self.num_display_images = 3  # 每个行显示的图片数量
        self.num_classify_buttons = 4  # 分类按钮数量

        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_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)
        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, 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))
            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])
                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[i].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)):
                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])
                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[i].append(label)

    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

        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, 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)

修改按钮大小

为了修改按钮的大小,可以使用 Buttonwidthheight 属性来设置按钮的宽度和高度。在 __init__ 方法中,找到创建按钮的地方,将 widthheight 属性添加到 Button 的参数中来设置按钮的大小。

例如,将加载文件夹按钮的大小设置为 100x50 像素:

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)

同样地,你也可以在其他按钮的创建处使用相同的方式来修改按钮的大小。根据你的需要,可以为每个按钮设置不同的大小。

说明

该代码实现了以下功能:

  • 加载文件夹中的图片:使用 filedialog.askdirectory() 选择文件夹,并读取文件夹中所有图片文件路径。
  • 显示图片:使用 Image.open()ImageTk.PhotoImage() 打开并显示图片。
  • 滚动显示图片:可以使用 “上一组” 和 “下一组” 按钮滚动显示图片。
  • 分类图片:设置多个分类按钮,点击按钮可以将当前显示的图片移动到指定的文件夹。
  • 调整按钮大小:可以使用 widthheight 属性调整按钮的大小。

希望这个代码能帮助你完成图片分类任务。

Python 图片分类工具 - 使用 tkinter 界面进行图片分类和管理

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

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