Created
October 7, 2016 18:36
-
-
Save petrklus/f852de77c4e02ad5c17f190930129b6d to your computer and use it in GitHub Desktop.
Downsize all JPEG photos and sort according to portrait/landscape orientation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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