Created
September 26, 2012 16:26
-
-
Save andrewxhill/3789005 to your computer and use it in GitHub Desktop.
Converts a directory of images into two directories, one large, one small of PNGs
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
import sys | |
from os import listdir, path, makedirs | |
from PIL import Image | |
from datetime import datetime | |
try: | |
import simplejson as json | |
except: | |
import json | |
SUPPORTED_FORMATS = ['.tif','.tiff','.png','.jpg'] | |
LARGE_OUTPUT_WIDTH = 1540 | |
SMALL_OUTPUT_WIDTH = 770 | |
METADATA = {} | |
def get_images(imdir): | |
# returns an array of all image names | |
filenames = [] | |
for files in listdir(imdir): | |
fileName, fileExtension = path.splitext(files) | |
if fileExtension.lower() in SUPPORTED_FORMATS: | |
filenames.append( files ) | |
return filenames | |
def resize_image(infile, indir, outdir): | |
fileName, fileExtension = path.splitext(infile) | |
im = Image.open("%s/%s" % (indir,infile)) #.convert("RGB") | |
data = {} | |
data['original'] = {} | |
data['original']['path'] = path.splitext(infile) | |
data['original']['name'] = fileName+fileExtension | |
data['original']['width'] = im.size[0] | |
data['original']['height'] = im.size[1] | |
largePercent = (LARGE_OUTPUT_WIDTH / float(im.size[0])) | |
largeHeight = int((float(im.size[1]) * float(largePercent))) | |
largeIm = im.resize((LARGE_OUTPUT_WIDTH,largeHeight), Image.ANTIALIAS) | |
largeIm.save("%s/large/%s.large.png" % (outdir,fileName), "PNG") | |
data['large'] = {} | |
data['large']['name'] = "large/%s.large.png" % fileName | |
data['large']['width'] = largeIm.size[0] | |
data['large']['height'] = largeIm.size[1] | |
smallPercent = (SMALL_OUTPUT_WIDTH / float(im.size[0])) | |
smallHeight = int((float(im.size[1]) * float(smallPercent))) | |
smallIm = im.resize((SMALL_OUTPUT_WIDTH,smallHeight), Image.ANTIALIAS) | |
smallIm.save("%s/small/%s.small.png" % (outdir,fileName), "PNG") | |
data['small'] = {} | |
data['small']['name'] = "small/%s.small.png" % fileName | |
data['small']['width'] = smallIm.size[0] | |
data['small']['height'] = smallIm.size[1] | |
METADATA['images'].append(data) | |
sys.stdout.write( "." ) | |
def main(): | |
args = [] | |
for arg in sys.argv: | |
args.append(arg) | |
try: | |
in_dir = args[1].strip().rstrip('/') | |
except: | |
in_dir = 'test' | |
try: | |
out_dir = args[2].strip().rstrip('/') | |
if not path.exists("%s" % out_dir): | |
print """WARNING - The output path you provided does not exist: %s""" % out_dir | |
except: | |
out_dir = 'out' | |
if not path.exists("%s/large" % out_dir): | |
makedirs("%s/large" % out_dir) | |
if not path.exists("%s/small" % out_dir): | |
makedirs("%s/small" % out_dir) | |
logfile = open("%s/details.js" % out_dir, "w+") | |
src_images = get_images(in_dir) | |
METADATA['sourceDir'] = in_dir | |
METADATA['targetDir'] = out_dir | |
METADATA['created_at'] = datetime.now().strftime("%A %d. %B %Y") | |
METADATA['highResDir'] = "%s/large" % out_dir | |
METADATA['lowResDir'] = "%s/small" % out_dir | |
METADATA['total'] = len(src_images) | |
METADATA['highResWidth'] = LARGE_OUTPUT_WIDTH | |
METADATA['highResWidth'] = SMALL_OUTPUT_WIDTH | |
METADATA['images'] = [] | |
print "-------------------------------------------" | |
print """Found %s images in the '%s/' directory""" % (len(src_images),in_dir) | |
print "-------------------------------------------" | |
sys.stdout.write( "Converting images" ) | |
for im in src_images: | |
resize_image(im, in_dir, out_dir) | |
print "!" | |
print "-------------------------------------------" | |
print """Large copies (%s) written to %s/large/""" % (len(src_images),out_dir) | |
print "-------------------------------------------" | |
print """Small copies (%s) written to %s/small/""" % (len(src_images),out_dir) | |
print "-------------------------------------------" | |
print """Writing details.js to %s""" % (out_dir) | |
logfile.write(json.dumps(METADATA, sort_keys=False, indent=4)) | |
logfile.close() | |
print "-------------------------------------------" | |
print """Converstion complete""" | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment