Skip to content

Instantly share code, notes, and snippets.

@petrklus
Created October 7, 2016 18:36
Show Gist options
  • Save petrklus/f852de77c4e02ad5c17f190930129b6d to your computer and use it in GitHub Desktop.
Save petrklus/f852de77c4e02ad5c17f190930129b6d to your computer and use it in GitHub Desktop.
Downsize all JPEG photos and sort according to portrait/landscape orientation
from PIL import Image
import glob
import os
files = glob.glob("source_folder/*.jpg")
output_size = 2048, 2048
for filename in files:
try:
im = Image.open(filename)
width, height = im.size
print width, height
orientation = "portrait" if width < height else "landscape"
outfile = os.path.join("output", orientation, os.path.split(filename)[-1])
# now convert size
im.thumbnail(output_size, Image.ANTIALIAS)
im.save(outfile, "JPEG")
except IOError:
print "cannot create thumbnail for '%s'" % infile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment