Last active
August 29, 2015 14:09
-
-
Save franklin-e/216e8be0e520d530cbdc to your computer and use it in GitHub Desktop.
dupes.py: take jpeg and integers, intelligently create duplicates at indicated sizes
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 __future__ import division | |
import os, sys | |
from PIL import Image | |
# Takes jpeg and a series of integers, intelligently creates duplicates at indicated sizes. | |
# Also replaces spaces with underscores and lowercases filenames. | |
# Usage: | |
# $ python dupes.py My\ Fabulous\ Image.jpg 1200 800 500 | |
# created my_fabulous_image_1200.jpg | |
# created my_fabulous_image_800.jpg | |
# created my_fabulous_image_500.jpg | |
source = sys.argv[1] | |
sizes = sys.argv[2:] | |
i = Image.open(source) | |
oldPath = os.path.dirname(source) | |
baseName, ext = os.path.splitext(os.path.basename(source)) # ext not needed | |
newBaseName = baseName.replace(' ', '_').lower() | |
width = i.size[0] | |
height = i.size[1] | |
if (oldPath): oldPath += '/' # path needs a trailing slash if it exists | |
for s in sizes: | |
s = int(s) | |
if width > height: | |
newSize = (s, int((height * (s / width)))) | |
else: | |
newSize = (int((width * (s / height))), s) | |
newImage = i.resize(newSize, Image.ANTIALIAS) | |
newPath = newBaseName + '_' + str(s) + '.jpg' | |
newImage.save(oldPath + newPath, "JPEG", quality=90) | |
print('created ' + oldPath + newPath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment