Skip to content

Instantly share code, notes, and snippets.

@franklin-e

franklin-e/dupes.py

Last active Aug 29, 2015
Embed
What would you like to do?
dupes.py: take jpeg and integers, intelligently create duplicates at indicated sizes
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
You can’t perform that action at this time.