Skip to content

Instantly share code, notes, and snippets.

@lostsh
Last active September 4, 2025 10:28
Show Gist options
  • Select an option

  • Save lostsh/0e508510458219ac70c6c5cdfa01b10c to your computer and use it in GitHub Desktop.

Select an option

Save lostsh/0e508510458219ac70c6c5cdfa01b10c to your computer and use it in GitHub Desktop.
Fast script to manually sort many images, but fast (avoid waiting for the image to load slowly)
import argparse
from PIL import Image, ImageTk, ImageOps
from os import listdir, path
import tkinter as tk
#from pynput import keyboard # later user event handling and classes
# List all pictures in the directory
# launch viewer on the first
# left right, for previous and next
# up down for ok not ok list, write the list into files to sort
def get_picture_list(path: str) -> list:
files = listdir(path)
return sorted(files)
def save_list(file: str, items: list) -> None:
fd = open(file, 'w+')
fd.write("\n".join(item for item in items))
fd.close()
def open_directory(directory: str, output_file: str) -> None:
pic_list = get_picture_list(directory)
pic_list_size = len(pic_list)
pictures_keep = list()
win = tk.Tk()
win.geometry("800x800")
i=0
while(i < pic_list_size):
current_file = path.join(directory, pic_list[i])
print("Opening: ", current_file)
print("[ > ]\tN°%d/%d (%d%%)" % (i, pic_list_size, ((i*100)/pic_list_size)))
#loading n showing img
pic_file = Image.open(current_file)
pic_file = ImageOps.contain(pic_file, (win.winfo_width(), win.winfo_height()))
im = ImageTk.PhotoImage(pic_file)
pane = tk.Label(win, image=im)
pane.grid(row=0, column=0)
win.update_idletasks()
#inputing commands
print(">", end='')
c = input()
if(c == 'k'):
print("+", end='')
pictures_keep.append(current_file)
elif(c == 'p'):
i=i-2
# if the prev was keept we remove it
if(path.join(directory, pic_list[i+1]) in pictures_keep):
pictures_keep.remove(path.join(directory, pic_list[i+1]))
elif(c == 'q'):
i = pic_list_size+1
else:
print("-", end='')
i+=1
print("[ < ]\tOK")
if(output_file != None):
save_list(output_file, pictures_keep)
print("Files to keep ({} of {}):\n".format(len(pictures_keep), pic_list_size), pictures_keep)
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="picsort", description="sort pictures")
parser.add_argument('-d', '--directory', type=str, help="Directory to load and sort", required=True)
parser.add_argument('-o', '--output', type=str, help="List of files to keep", required=False)
args = parser.parse_args()
print("Welcome to pic sorter\nUsage:\n\tn\tnext\n\tk\tkeep\n\tj\tdelete")
open_directory(args.directory, args.output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment