{"title": "图片分类工具 - Python GUI 实现", "description": "使用 Python 的 tkinter 库构建一个图片分类工具,用于快速将图片分类到不同的文件夹。", "keywords": "图片分类, GUI, Python, tkinter, 文件夹", "content": "import os\nimport shutil\nimport tkinter as tk\nfrom tkinter import filedialog\nfrom PIL import Image, ImageTk\n\nclass ImageClassifier:\n def init(self, root):\n self.root = root\n self.root.title("图片分类工具")\n self.root.geometry("800x500")\n self.root.bind("", self.key_press_event)\n\n self.image_paths = []\n self.current_index = 0\n self.display_images = []\n self.display_images_buttons = []\n self.num_display_images = 3 # Number of images to display in each row\n self.num_classify_buttons = 4 # Number of classify buttons\n\n self.image_frame = tk.Frame(self.root)\n self.image_frame.pack(pady=50)\n\n self.button_frame = tk.Frame(self.root)\n self.button_frame.pack(pady=10)\n\n self.save_buttons_frame = tk.Frame(self.root)\n self.save_buttons_frame.pack(pady=10)\n\n self.save_paths = []\n self.save_path_entries = []\n self.save_buttons = []\n\n self.next_button = tk.Button(self.button_frame, text="下一组", command=self.next_images, width=10, height=2)\n self.next_button.pack(side=tk.RIGHT, padx=10)\n\n self.prev_button = tk.Button(self.button_frame, text="上一组", command=self.prev_images, width=10, height=2)\n self.prev_button.pack(side=tk.RIGHT, padx=10)\n\n self.load_button = tk.Button(self.button_frame, text="打开图片", command=self.load_folder, width=10, height=2)\n self.load_button.pack(side=tk.LEFT, padx=10)\n\n self.classify_buttons_frame = tk.Frame(self.root)\n self.classify_buttons_frame.pack(pady=10)\n\n self.classify_buttons = []\n for i in range(self.num_classify_buttons):\n classify_button = tk.Button(self.classify_buttons_frame, text="类别 {}".format(i+1), command=lambda i=i: self.classify_images(i), width=10, height=2)\n classify_button.pack(side=tk.LEFT, padx=10)\n self.classify_buttons.append(classify_button)\n\n for i in range(self.num_display_images):\n self.display_images_buttons.append([])\n\n self.root.mainloop()\n\n def load_folder(self):\n folder_path = filedialog.askdirectory()\n self.image_paths = []\n for file_name in sorted(os.listdir(folder_path)):\n if file_name.lower().endswith(('.png', '.jpg', '.jpeg', '.gif')):\n self.image_paths.append(os.path.join(folder_path, file_name))\n self.current_index = 0\n self.next_images()\n\n def next_images(self):\n if self.current_index + len(self.display_images) < len(self.image_paths):\n self.current_index += len(self.display_images)\n self.display_images = []\n for i in range(len(self.display_images_buttons)):\n for button in self.display_images_buttons[i]:\n button.destroy()\n self.display_images_buttons[i] = []\n for i in range(min(len(self.image_paths) - self.current_index, self.num_display_images)):\n self.display_images.append(self.image_paths[self.current_index + i])\n image = Image.open(self.image_paths[self.current_index + i])\n # Display the image using PIL\n image = image.resize(image.size)\n photo = ImageTk.PhotoImage(image)\n button = tk.Button(self.image_frame, image=photo, command=lambda i=i: self.classify_images(i))\n button.photo = photo\n button.pack(side=tk.LEFT, padx=10)\n self.display_images_buttons[i].append(button)\n\n def prev_images(self):\n if self.current_index - len(self.display_images) >= 0:\n self.current_index -= len(self.display_images)\n self.display_images = []\n for i in range(len(self.display_images_buttons)):\n for button in self.display_images_buttons[i]:\n button.destroy()\n self.display_images_buttons[i] = []\n for i in range(min(len(self.image_paths) - self.current_index, self.num_display_images)):\n self.display_images.append(self.image_paths[self.current_index + i])\n image = Image.open(self.image_paths[self.current_index + i])\n # Display the image using PIL\n image = image.resize(image.size)\n photo = ImageTk.PhotoImage(image)\n button = tk.Button(self.image_frame, image=photo, command=lambda i=i: self.classify_images(i))\n button.photo = photo\n button.pack(side=tk.LEFT, padx=10)\n self.display_images_buttons[i].append(button)\n\n def classify_images(self, index):\n button = self.classify_buttons[index]\n if hasattr(button, 'save_path'):\n folder_path = button.save_path\n else:\n folder_path = filedialog.askdirectory()\n button.save_path = folder_path\n\n image_path = self.display_images[index]\n image_name = os.path.basename(image_path)\n new_path = os.path.join(folder_path, image_name)\n shutil.copyfile(image_path, new_path)\n print("图片 {} 移动到了 {}".format(image_name, new_path))\n\n def key_press_event(self, event):\n if event.char.lower() == 'a':\n self.prev_images()\n elif event.char.lower() == 'd':\n self.next_images()\n\n def save_images(self, index):\n folder_path = filedialog.askdirectory()\n self.save_paths[index] = folder_path\n self.save_path_entries[index].delete(0, tk.END)\n self.save_path_entries[index].insert(0, folder_path)\n\nroot = tk.Tk()\napp = ImageClassifier(root)

图片分类工具 - Python GUI 实现

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

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