Skip to content

Instantly share code, notes, and snippets.

@kalyan02
Created July 6, 2013 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kalyan02/5940821 to your computer and use it in GitHub Desktop.
Save kalyan02/5940821 to your computer and use it in GitHub Desktop.
A utility to resize files. Usage: $ python resize.py *.jpg -o resized/ -s 1280
#!/usr/bin/python
import Image
import os
import sys
import glob
from optparse import OptionParser
args = sys.argv
parser = OptionParser()
parser.add_option("-o", "--output", dest="output")
parser.add_option("-s", "--size", dest="size")
kwargs, inputs = parser.parse_args()
output_dir = kwargs.output
size = kwargs.size
is_percent = '%' in size
dest_w, dest_h = None, None
if '%' in size:
dest_w = size.strip().strip('%').strip()
try:
dest_w = int(dest_w)
except:
print "Error, need proper size format"
print "Format: --size=10%"
sys.exit(-1)
if 'x' in size:
dest_w, dest_h = size.split('x')
print dest_w, dest_h
if not dest_w or not dest_h:
if not is_percent:
try:
dest_w = int(size)
except:
print "Error, need proper size"
print "Possible options format: =100x100, =100, =50%"
sys.exit(-1)
if not os.path.exists(output_dir):
print "Need a valid existing directory"
sys.exit(-1)
for each in inputs:
img = Image.open(each)
resize_mode = Image.ANTIALIAS
imgr = None
if is_percent:
osize = img.size
imgr = img.resize( (osize[0]*dest_w/100, osize[0]*dest_w/100), resize_mode )
#elif dest_w and dest_h:
# # todo, need to fit it to bounding box
# pass
elif dest_w:
osize = img.size
_w, _h = dest_w, osize[1] * dest_w / osize[0]
imgr = img.resize( (_w,_h), resize_mode )
b_name = os.path.basename(each)
b_ext = b_name.split('.')[-1]
if b_ext.upper() == 'JPG':
b_ext = 'JPEG'
imgr.save( output_dir.rstrip('/') + "/" + b_name, format=b_ext )
print 'Resizing:', each
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment