Sign In

Faster Tagging GUI Script

Faster Tagging GUI Script

Sometimes you want to tag the pictures one by one, because your data set is a mixture of several distinct types. For example, I get 3000 minecraft skins from somewhere, and now I want to catergorize them by their eye color. Here I got a script from GPT.

import os
import shutil
from PIL import ImageTk, Image
import tkinter as tk
from tkinter import messagebox

class ImageClassifier:
    def __init__(self, master, img_dir, categories):
        self.master = master
        self.img_dir = img_dir
        self.categories = categories
        self.images = os.listdir(img_dir)
        self.image_index = 0

        self.setup_ui()

    def setup_ui(self):
        self.image_label = tk.Label(self.master)
        self.image_label.pack()

        for category in self.categories:
            button = tk.Button(self.master, text=category, command=lambda cat=category: self.classify_image(cat))
            button.pack(side=tk.LEFT)

        self.load_image()

    def load_image(self):
        while self.image_index < len(self.images):
            img_path = os.path.join(self.img_dir, self.images[self.image_index])
            if os.path.isfile(img_path):
                img = Image.open(img_path)
                img = img.resize((512, 512), Image.NEAREST) # adjust image size
                img = ImageTk.PhotoImage(img)
                self.image_label.configure(image=img)
                self.image_label.image = img
                break
            else:
                self.image_index += 1
        else:
            messagebox.showinfo("Info", "No more images to classify")


    def classify_image(self, category):
        src = os.path.join(self.img_dir, self.images[self.image_index])
        dest_dir = os.path.join(self.img_dir, category)
        os.makedirs(dest_dir, exist_ok=True)
        dest = os.path.join(dest_dir, self.images[self.image_index])

        shutil.move(src, dest)
        self.image_index += 1
        self.load_image()


if __name__ == "__main__":
    root = tk.Tk()
    app = ImageClassifier(root, "input", 
                          ["red", "blue", "orange", "yellow", "green", "cyan", "purple", "pink", "grey", "black", "white", "brown", "other"])
    root.mainloop()

Feel free to use it. Paste it into a file named sorter.py then python sorter.py, and you are ready to go.

Place the data into a folder called "input" and place this script alongside the "input" folder.

You may change the catergories by editing the "app = ImageClassifier" part. It is strongly recommended that you remain an "other" alongside your other options.

Whenever you click a button, that picture will be thrown into the corresponding sub folder like "input/red". If it does not exist, it will automatically create it.

3

Comments