sorter.py (910B)
1 import os 2 import shutil 3 4 # Create the 'images' directory if it doesn't exist 5 if not os.path.exists('images'): 6 os.makedirs('images') 7 8 # Initialize a counter for naming the images 9 counter = 1 10 11 # Function to move and rename images 12 def move_and_rename_image(src, ext): 13 global counter 14 new_name = f'cat{counter}.{ext}' 15 dest = os.path.join('images', new_name) 16 shutil.move(src, dest) 17 counter += 1 18 19 # Function to recursively search for images 20 def search_for_images(directory): 21 for root, _, files in os.walk(directory): 22 for filename in files: 23 if filename.lower().endswith(('.png', '.jpg', '.jpeg')): 24 image_path = os.path.join(root, filename) 25 _, ext = os.path.splitext(filename) 26 move_and_rename_image(image_path, ext.strip('.').lower()) 27 28 # Start searching for images from the current directory 29 search_for_images(os.getcwd())