Advertisement
creamygoat

Badger

Mar 28th, 2013
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.53 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. '''Puts a badge on an avatar image
  4.  
  5. NAME
  6.  badger
  7.  
  8. SYNOPSIS
  9.  badger avatar badge outputfile
  10.  
  11. DESCRIPTION
  12.  The badger script pastes a small copy of a badge image, such as the
  13.  marriage equality badge onto the lower-right part of an avatar
  14.  image (a Facebook profile picture, say) and creates a new image.
  15.  
  16.  The format of the output image is determined by the extention of
  17.  output image's file name. If that cannot be determined, the output
  18.  image will be a JPEG image.
  19.  
  20.  This script can be found on Pastebin.
  21.  http://pastebin.com/JxPUJRR6
  22.  
  23. '''
  24.  
  25. import sys
  26. import os.path
  27. from PIL import Image
  28.  
  29. def main():
  30.  
  31.   rc = 0
  32.  
  33.   facefname = None
  34.   badgefname = None
  35.   outfname = None
  36.  
  37.   args = list(sys.argv)
  38.   cn = args[0]
  39.  
  40.   if len(args) != 4:
  41.     print cn + ': Exacty three parameters are required.'
  42.     rc = 1
  43.   else:
  44.     facefname, badgefname, outfname = args[1:4]
  45.  
  46.   if rc:
  47.     return rc
  48.  
  49.   face = Image.open(facefname)
  50.   w, h = face.size
  51.  
  52.   margin = int(round(0.08 * min(w, h)))
  53.  
  54.   badge = Image.open(badgefname)
  55.   bh = int(round(h * 18.0 / 50.0))
  56.   bw = int(round(bh * float(w) / float(h)))
  57.   by = h - margin - bh
  58.   bx = w - margin - bw
  59.  
  60.   weebadge = badge.resize((bw, bh), Image.ANTIALIAS)
  61.   face.paste(weebadge, (bx, by))
  62.  
  63.   ext = os.path.splitext(outfname)[1]
  64.  
  65.   fmt = 'JPEG'
  66.   fmt = 'GIF' if ext in ['.gif', '.GIF'] else fmt
  67.   fmt = 'PNG' if ext in ['.png', '.PMG'] else fmt
  68.  
  69.   face.save(outfname, fmt)
  70.  
  71.   return rc
  72.  
  73. if __name__ == '__main__':
  74.   sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement