Advertisement
Guest User

Untitled

a guest
May 24th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. # Takes a jpeg by path, appends '_ig' to the basename,
  2. # centers it on a fitted, square, white background.
  3.  
  4. # Usage:
  5.  
  6. # $ python ig.py fabulous_image.jpg
  7. # created fabulous_image_ig.jpg
  8.  
  9. import Image, os, sys
  10.  
  11. source = sys.argv[1]
  12. img = Image.open(source, 'r')
  13. width, height = img.size
  14. bigger = max(img.size)
  15. if width > height:
  16. offset = (0, (bigger - height) / 2)
  17. else:
  18. offset = ((bigger - width) / 2, 0)
  19.  
  20. oldPath = os.path.dirname(source)
  21. baseName, ext = os.path.splitext(os.path.basename(source))
  22. newBaseName = baseName + '_ig'
  23. if (oldPath): oldPath += '/' # path needs a trailing slash if it exists
  24.  
  25. background = Image.new('RGBA', (bigger, bigger), (255, 255, 255, 255))
  26.  
  27. background.paste(img, offset)
  28. newFile = newBaseName + ext
  29. background.save(oldPath + newFile, 'JPEG', quality = 95)
  30. print('created ' + oldPath + newFile)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement