Last active
August 29, 2015 14:19
-
-
Save franklin-e/8c004a5bf748f9fbbdb0 to your computer and use it in GitHub Desktop.
make a jpeg Instagram-friendly
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
# 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