Skip to main content

sort imgs.py

edit the code for the folder

from PIL import Image
import os

unique_images = {}

# edit this
directory = r'J:\New folder'


for filename in os.listdir(directory):
    if filename.endswith('.png'):
        
        img = Image.open(os.path.join(directory, filename))
        
        if str(img.tobytes()) in unique_images:
            
            os.remove(os.path.join(directory, filename))
            print(f"Deleted {filename}")
        else:
            
            unique_images[str(img.tobytes())] = filename
            print(f"Kept {filename}")

this one will ask where you want to check

from PIL import Image
import os


unique_images = {}

directory = input("Enter the directory containing the PNG files: ")

for filename in os.listdir(directory):
    if filename.endswith('.png'):
        
        img = Image.open(os.path.join(directory, filename))

        if str(img.tobytes()) in unique_images:

            os.remove(os.path.join(directory, filename))
            print(f"Deleted {filename}")
        else:
            
            unique_images[str(img.tobytes())] = filename
            print(f"Kept {filename}")