Created
January 9, 2013 01:54
-
-
Save agea/4489900 to your computer and use it in GitHub Desktop.
Resize all images in folder an descendants
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
#!/usr/bin/python | |
import sys, os, logging, math, ConfigParser, random,inspect | |
import Image, ImageStat | |
frameSize = (720,480) | |
frameRatio = float(frameSize[0])/float(frameSize[1]) | |
def files(folder): | |
for path, folders, files in os.walk(folder): | |
for name in files: | |
yield os.path.join(path, name) | |
def buildTile(image): | |
width=frameSize[0] | |
height=frameSize[1] | |
size=frameSize | |
imageRatio = float(image.size[0])/float(image.size[1]) | |
if frameRatio==imageRatio: | |
newSize=size | |
elif frameRatio < imageRatio: | |
newSize=(int(imageRatio*height),int(height)) | |
else: | |
newSize=(int(width),int(width/imageRatio)) | |
tile = image.resize(newSize,Image.ANTIALIAS) | |
l = int(newSize[0]-size[0])/2 | |
u = int(newSize[1]-size[1])/2 | |
box=(l,u,l+size[0],u+size[1]) | |
if box != (0,0,size[0],size[1]): | |
tile = tile.crop(box) | |
return tile | |
folder = sys.argv[1] | |
for file in files(folder): | |
try: | |
image = Image.open(file) | |
if image.size!=frameSize: | |
tile = buildTile(image) | |
tile.save(file,quality=95) | |
print "resized",file | |
else: | |
print "already resized",file | |
except Exception,e: | |
print file, e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment