图片分类工具:批量整理图片到不同文件夹
这是一个使用Python Tkinter开发的图片分类工具,可以帮助用户快速将图片分类到不同的文件夹。用户可以选择文件夹,工具会自动显示图片,并提供分类按钮,用户可以通过点击分类按钮将图片移动到相应的文件夹。
import os
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
import shutil # 导入shutil模块
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_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)
使用说明:
- 运行代码,点击“加载文件夹”按钮选择要分类的图片所在的文件夹。
- 工具会自动显示该文件夹中的图片(最多显示4张)。
- 点击不同的分类按钮,可以将当前显示的图片移动到相应的文件夹。
- 点击“保存”按钮可以将当前显示的图片保存到指定文件夹。
注意:
- 代码中设置了4个分类按钮,可以根据需要修改。
- 使用该工具分类图片时,请确保目标文件夹存在。
- 如果目标文件夹不存在,工具会尝试创建目标文件夹。
希望这个图片分类工具可以帮助到您!
原文地址: https://www.cveoy.top/t/topic/qE7U 著作权归作者所有。请勿转载和采集!