Skip to content

Instantly share code, notes, and snippets.

@encima
Last active September 14, 2020 19:49
Show Gist options
  • Save encima/5d7b842f2156dc5bf4c5 to your computer and use it in GitHub Desktop.
Save encima/5d7b842f2156dc5bf4c5 to your computer and use it in GitHub Desktop.
Resizes a set of images found in a directory|-|{"files":{"resize.py":{"env":"plain"}},"tag":"Python"}
# Author: Chris Gwilliams
# Date: 22/7/14
# Usage: python resize.py [dir]
# Requires: PIL
# Purpose grabs every image in specified dir (and subdirs) and resizes according to the size variable
import os, sys
import Image
# set your size here, PIL will try and make it a bit smarter
size = 1280, 720
for root,dirs,files in os.walk(sys.argv[1]):
for f in files:
if(f.lower().endswith('jpg') or f.lower().endswith('.png')):
infile = root + '/' + f
if('thumbnail.png' not in infile):
outfile = root + '/' + f + ".thumbnail.png"
if infile != outfile:
im = Image.open(infile)
width, height = im.size
if height > width:
size = 720, 1280
im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, 'PNG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment