Skip to content

Instantly share code, notes, and snippets.

@DLohn
Last active February 29, 2024 22:57
Show Gist options
  • Save DLohn/3d613976e2d19940f6473045fa3900b0 to your computer and use it in GitHub Desktop.
Save DLohn/3d613976e2d19940f6473045fa3900b0 to your computer and use it in GitHub Desktop.
Resize all images in a directory
import sys
import os
from PIL import Image
resize_to = 384
target_dir = sys.argv[1]
for file in os.listdir(target_dir):
full_path = os.path.join(target_dir, file)
try:
im = Image.open(full_path)
except:
continue
if im.width > im.height:
new_size = (resize_to, int(resize_to * (im.height/im.width)))
else:
new_size = (int(resize_to * (im.width/im.height)), resize_to)
im_resized = im.resize(new_size)
im_resized.save(full_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment