Skip to content

Instantly share code, notes, and snippets.

@jubilancy
Created June 17, 2026 15:07
Show Gist options
  • Select an option

  • Save jubilancy/1d1ceea98100cec4d97e927b2b251136 to your computer and use it in GitHub Desktop.

Select an option

Save jubilancy/1d1ceea98100cec4d97e927b2b251136 to your computer and use it in GitHub Desktop.
this script groups them into 6 standard digital buckets (Square, Portrait, Landscape, Ultrawide) plus an "Other" catch-all, keeping your final folder count down to around 10 or fewer.
from PIL import Image
import os
import shutil
def get_aspect_ratio(image_path):
with Image.open(image_path) as img:
width, height = img.size
aspect_ratio = float(width) / float(height)
return aspect_ratio
def get_all_aspect_ratios(directory):
image_files = [f for f in os.listdir(directory) if
f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff'))]
aspect_ratios = {}
for image_file in image_files:
image_path = os.path.join(directory, image_file)
try:
aspect_ratio = get_aspect_ratio(image_path)
aspect_ratios[image_file] = aspect_ratio
except Exception:
continue
return aspect_ratios
def get_bucket_name(ratio):
# Match the ratio against common standard buckets with a small tolerance window
if 0.95 <= ratio <= 1.05:
return "1_square_1-1"
# Landscapes (Width > Height)
elif 1.25 <= ratio <= 1.40:
return "2_landscape_4-3"
elif 1.41 <= ratio <= 1.60:
return "3_landscape_3-2"
elif 1.61 <= ratio <= 1.85:
return "4_landscape_16-9"
elif ratio > 1.85:
return "5_landscape_ultrawide"
# Portraits (Height > Width)
elif 0.70 <= ratio <= 0.80:
return "6_portrait_3-4"
elif 0.60 <= ratio <= 0.69:
return "7_portrait_2-3"
elif 0.50 <= ratio <= 0.59:
return "8_portrait_16-9"
elif ratio < 0.50:
return "9_portrait_tall"
else:
return "10_other_ratios"
if __name__ == "__main__":
directory_path = "./"
aspect_ratios = get_all_aspect_ratios(directory_path)
grouped_buckets = {}
for image_file, aspect_ratio in aspect_ratios.items():
bucket_name = get_bucket_name(aspect_ratio)
if bucket_name not in grouped_buckets:
grouped_buckets[bucket_name] = []
grouped_buckets[bucket_name].append(image_file)
print("Organizing images into standard ratio buckets...")
for bucket, files in grouped_buckets.items():
folder_name = os.path.join(directory_path, bucket)
if not os.path.exists(folder_name):
os.makedirs(folder_name)
for file in files:
source_path = os.path.join(directory_path, file)
destination_path = os.path.join(folder_name, file)
shutil.move(source_path, destination_path)
print(f"Moved {file} -> {folder_name}/")
print("\nSorting into main buckets complete!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment