Skip to content

Instantly share code, notes, and snippets.

@lucascompython
Last active February 24, 2022 21:27
Show Gist options
  • Save lucascompython/b0648a30386cc049e3cd3b5fbbf141c4 to your computer and use it in GitHub Desktop.
Save lucascompython/b0648a30386cc049e3cd3b5fbbf141c4 to your computer and use it in GitHub Desktop.
This is a very simple image resizer that keeps the aspect ratio.
import os, sys
from PIL import Image
def rezise(dir, size: tuple = (768, 512)) -> None:
for infile in os.listdir(dir):
outfile = os.path.splitext(infile)[0] + "_resized.thumbnail"
if infile != outfile:
try:
with Image.open(os.path.join(dir, infile)) as im:
cp = im.copy()
cp.thumbnail(size, Image.ANTIALIAS)
cp.save(os.path.join(dir, outfile), "JPEG")
print("Resized: " + infile)
except IOError:
print("Cannot resize " + infile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment