Skip to content

Instantly share code, notes, and snippets.

@ggorlen
Last active April 12, 2021 22:20
Show Gist options
  • Select an option

  • Save ggorlen/e7cb3f58f034fa0d7de21fc21f3a7ae4 to your computer and use it in GitHub Desktop.

Select an option

Save ggorlen/e7cb3f58f034fa0d7de21fc21f3a7ae4 to your computer and use it in GitHub Desktop.
batch resize photos
import os
from PIL import Image
def main():
# TODO https://stackoverflow.com/questions/4228530/pil-thumbnail-is-rotating-my-image
in_dir = "."
out_dir = "thumbs"
max_dimensions = 350, 350
extension = ".jpg"
postfix = "_thumb.jpg"
if not os.path.isdir(out_dir):
os.mkdir(out_dir)
for item in os.listdir(in_dir):
path = os.path.join(in_dir, item)
if os.path.isfile(path) and item.endswith(extension):
img = Image.open(path)
fname, _ = os.path.splitext(path)
img.thumbnail(max_dimensions, Image.ANTIALIAS)
img.save(os.path.join(out_dir, fname + postfix), "JPEG", quality=90)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment