1 #!/usr/bin/python 2 3 import sys 4 import StringIO 5 try: 6 import Image, ImageDraw, ImageFont 7 except ImportError, e: 8 print str(e) 9 print "Are you missing the Python Imaging Library? (apt-get install python-imaging)" 10 sys.exit(1) 11 12 FONT_PATH = "/usr/share/fonts/truetype/msttcorefonts/arial.ttf" 13 14 class Args(object): 15 def __init__(self, dest_name, size, circle_color, border_color, 16 letter_color, letter): 17 self.dest_name = dest_name 18 self.size = size 19 self.circle_color = circle_color 20 self.border_color = border_color 21 self.letter_color = letter_color 22 self.letter = letter 23 24 def main(args): 25 data = process_args(args) 26 if data: 27 createImage(data) 28 29 def process_args(args): 30 if not args or len(args) != 6: 31 usage() 32 return Args(*args) 33 34 def usage(): 35 print """Usage: %s <file_name> <size> <circle-color> <border-color> <letter-color> <letter>""" % sys.argv[0] 36 sys.exit(1) 37 38 def createImage(data): 39 zoom = 4 40 rmin = -zoom/2 41 rmax = zoom/2 42 if zoom > 1: 43 r = range(-zoom/2, zoom/2+1) 44 else: 45 r = [ 0 ] 46 sz = int(data.size) 47 sz4 = sz * zoom 48 49 img = Image.new("RGBA", (sz4, sz4), (255,255,255,0)) 50 draw = ImageDraw.Draw(img) 51 52 draw.ellipse((0, 0, sz4-zoom, sz4-zoom), 53 fill=data.circle_color, outline=None) 54 for i in r: 55 draw.ellipse((i, i, sz4-i-zoom, sz4-i-zoom), 56 fill=None, outline=data.border_color) 57 58 font = ImageFont.truetype(FONT_PATH, int(sz4 * .75)) 59 tsx, tsy = draw.textsize(data.letter, font=font) 60 61 ptx = (sz4 - tsx) / 2 62 pty = (sz4 - tsy) / 2 63 for i in r: 64 draw.text((ptx + i, pty), data.letter, font=font, fill=data.letter_color) 65 66 img = img.resize((sz, sz), Image.BICUBIC) 67 img.save(data.dest_name, "PNG") 68 print "Saved", data.dest_name 69 70 if __name__ == "__main__": 71 main(sys.argv[1:]) 72