Skip to content

Instantly share code, notes, and snippets.

@franklin-e
Last active August 29, 2015 14:19
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 franklin-e/8c004a5bf748f9fbbdb0 to your computer and use it in GitHub Desktop.
Save franklin-e/8c004a5bf748f9fbbdb0 to your computer and use it in GitHub Desktop.
make a jpeg Instagram-friendly
# Takes a jpeg by path, appends '_ig' to the basename,
# centers it on a fitted, square, white background.
# Usage:
# $ python ig.py fabulous_image.jpg
# created fabulous_image_ig.jpg
import Image, os, sys
source = sys.argv[1]
img = Image.open(source, 'r')
width, height = img.size
bigger = max(img.size)
if width > height:
offset = (0, (bigger - height) / 2)
else:
offset = ((bigger - width) / 2, 0)
oldPath = os.path.dirname(source)
baseName, ext = os.path.splitext(os.path.basename(source))
newBaseName = baseName + '_ig'
if (oldPath): oldPath += '/' # path needs a trailing slash if it exists
background = Image.new('RGBA', (bigger, bigger), (255, 255, 255, 255))
background.paste(img, offset)
newFile = newBaseName + ext
background.save(oldPath + newFile, 'JPEG', quality = 95)
print('created ' + oldPath + newFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment