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)