Image Classification Tool: Automatic Batch Processing
import\u0020tkinter\u0020as\u0020tk\nfrom\u0020PIL\u0020import\u0020ImageTk,\u0020Image\nimport\u0020os\nimport\u0020shutil\n\n\nclass\u0020ImageClassificationTool:\n\tdef\u0020__init__(self,\u0020root):\n\t self.root\u0020=\u0020root\n\t self.root.title("Image\u0020Classification\u0020Tool")\n\n\t self.image_frame\u0020=\u0020tk.Frame(self.root)\n\t self.image_frame.pack()\n\t self.image_labels\u0020=\u0020[]\n\n\t self.button_frame\u0020=\u0020tk.Frame(self.root)\n\t self.button_frame.pack()\n\n\t self.image_paths\u0020=\u0020[]\n\t self.current_image_index\u0020=\u00200\n\n\t self.display_count\u0020=\u00202\n\n\t self.load_images_from_folder()\n\t self.display_images()\n\n\t self.create_buttons()\n\n\tdef\u0020load_images_from_folder(self):\n\t folder_path\u0020=\u0020"D:\p\cw\C06center"\u0020#\u0020更改为你的文件夹路径\n\t self.image_paths\u0020=\u0020sorted(\n\t [os.path.join(folder_path,\u0020file)\u0020for\u0020file\u0020in\u0020os.listdir(folder_path)\u0020if\u0020file.endswith(".jpg")]\n\t )\n\n\tdef\u0020display_images(self):\n\t for\u0020label\u0020in\u0020self.image_labels:\n\t label.destroy()\n\t self.image_labels\u0020=\u0020[]\n\n\t for\u0020i\u0020in\u0020range(self.current_image_index,\u0020self.current_image_index\u0020+\u0020self.display_count):\n\t if\u0020i\u0020<\u0020len(self.image_paths):\n\t image_path\u0020=\u0020self.image_paths[i]\n\t img\u0020=\u0020Image.open(image_path)\n\t img\u0020=\u0020img.resize((200,\u0020200),\u0020Image.ANTIALIAS)\n\t photo\u0020=\u0020ImageTk.PhotoImage(img)\n\t label\u0020=\u0020tk.Label(self.image_frame,\u0020image=photo)\n\t label.image\u0020=\u0020photo\n\t label.grid(row=0,\u0020column=i\u0020-\u0020self.current_image_index)\n\t self.image_labels.append(label)\n\n\tdef\u0020create_buttons(self):\n\t folder_paths\u0020=\u0020["D:\p\cw\d",\u0020"D:\p\cw\p",\u0020"D:\p\cw\q",\u0020"D:\p\cw\w"]\u0020#\u0020更改为你需要的文件夹路径\n\t self.button_index\u0020=\u00200\n\t button\u0020=\u0020tk.Button(self.button_frame,\u0020text=f"Folder\u0020{self.button_index\u0020+\u00201}",\u0020command=self.process_images)\n\t button.pack(side="left")\n\n\tdef\u0020process_images(self):\n\t folder_paths\u0020=\u0020["D:\p\cw\d",\u0020"D:\p\cw\p",\u0020"D:\p\cw\q",\u0020"D:\p\cw\w"]\u0020#\u0020更改为你需要的文件夹路径\n\t folder_path\u0020=\u0020folder_paths[self.button_index]\n\t os.makedirs(folder_path,\u0020exist_ok=True)\n\t for\u0020i\u0020in\u0020range(self.current_image_index,\u0020self.current_image_index\u0020+\u0020self.display_count):\n\t if\u0020i\u0020<\u0020len(self.image_paths):\n\t image_path\u0020=\u0020self.image_paths[i]\n\t shutil.copy(image_path,\u0020folder_path)\n\t print(f"Image\u0020{image_path}\u0020saved\u0020to\u0020Folder\u0020{self.button_index\u0020+\u00201}")\n\t self.button_index\u0020+=\u00201\n\t if\u0020self.button_index\u0020<\u0020len(folder_paths):\n\t button\u0020=\u0020tk.Button(self.button_frame,\u0020text=f"Folder\u0020{self.button_index\u0020+\u00201}",\u0020command=self.process_images)\n\t button.pack(side="left")\n\t self.current_image_index\u0020+=\u0020self.display_count\n\t if\u0020self.current_image_index\u0020>=\u0020len(self.image_paths):\n\t print("All\u0020images\u0020processed.")\n\t else:\n\t self.process_next_batch()\n\t self.display_images()\n\n\tdef\u0020process_next_batch(self):\n\t print("Processing\u0020next\u0020batch...")\n\t self.root.after(2000,\u0020self.process_images)\n\n\nroot\u0020=\u0020tk.Tk()\napp\u0020=\u0020ImageClassificationTool(root)\nroot.mainloop()\n
原文地址: https://www.cveoy.top/t/topic/qE7k 著作权归作者所有。请勿转载和采集!