Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 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)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement