Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- '''Puts a badge on an avatar image
- NAME
- badger
- SYNOPSIS
- badger avatar badge outputfile
- DESCRIPTION
- The badger script pastes a small copy of a badge image, such as the
- marriage equality badge onto the lower-right part of an avatar
- image (a Facebook profile picture, say) and creates a new image.
- The format of the output image is determined by the extention of
- output image's file name. If that cannot be determined, the output
- image will be a JPEG image.
- This script can be found on Pastebin.
- http://pastebin.com/JxPUJRR6
- '''
- import sys
- import os.path
- from PIL import Image
- def main():
- rc = 0
- facefname = None
- badgefname = None
- outfname = None
- args = list(sys.argv)
- cn = args[0]
- if len(args) != 4:
- print cn + ': Exacty three parameters are required.'
- rc = 1
- else:
- facefname, badgefname, outfname = args[1:4]
- if rc:
- return rc
- face = Image.open(facefname)
- w, h = face.size
- margin = int(round(0.08 * min(w, h)))
- badge = Image.open(badgefname)
- bh = int(round(h * 18.0 / 50.0))
- bw = int(round(bh * float(w) / float(h)))
- by = h - margin - bh
- bx = w - margin - bw
- weebadge = badge.resize((bw, bh), Image.ANTIALIAS)
- face.paste(weebadge, (bx, by))
- ext = os.path.splitext(outfname)[1]
- fmt = 'JPEG'
- fmt = 'GIF' if ext in ['.gif', '.GIF'] else fmt
- fmt = 'PNG' if ext in ['.png', '.PMG'] else fmt
- face.save(outfname, fmt)
- return rc
- if __name__ == '__main__':
- sys.exit(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement