import osimport shutilimport tkinter as tkfrom tkinter import filedialogfrom PIL import Image ImageTkclass ImageClassifier def __init__self root selfroot = root selfroottitle图片分类工具
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 = [] # <-- Add this line
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_entries = []
self.save_path_labels = []
self.save_buttons = []
for i in range(4):
save_path_entry = tk.Entry(self.save_button_frame, width=50)
save_path_entry.pack(side=tk.LEFT, padx=10)
save_path_entry.insert(0, "保存路径")
self.save_path_entries.append(save_path_entry)
save_path_label = tk.Label(self.save_button_frame, text="保存路径 {}:".format(i+1))
save_path_label.pack(side=tk.LEFT, padx=10)
self.save_path_labels.append(save_path_label)
save_button = tk.Button(self.save_button_frame, text="保存", command=lambda i=i: self.save_images(i))
save_button.pack(side=tk.LEFT, padx=10)
self.save_buttons.append(save_button)
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 = self.save_path_entries[index].get()
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 = self.save_path_entries[index].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
原文地址: http://www.cveoy.top/t/topic/iY5U 著作权归作者所有。请勿转载和采集!