aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse/scripts/gen_icon.py
blob: f6274e16e8f335771191180eefe15775adc3db8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/python

import sys
import StringIO
try:
    import Image, ImageDraw, ImageFont
except ImportError, e:
    print str(e)
    print "Are you missing the Python Imaging Library? (apt-get install python-imaging)"
    sys.exit(1)

FONT_PATH = "/usr/share/fonts/truetype/msttcorefonts/arial.ttf"

class Args(object):
    def __init__(self, dest_name, size, circle_color, border_color,
                 letter_color, letter):
        self.dest_name = dest_name
        self.size = size
        self.circle_color = circle_color
        self.border_color = border_color
        self.letter_color = letter_color
        self.letter = letter

def main(args):
    data = process_args(args)
    if data:
        createImage(data)

def process_args(args):
    if not args or len(args) != 6:
        usage()
    return Args(*args)
    
def usage():
    print """Usage: %s <file_name> <size> <circle-color> <border-color> <letter-color> <letter>""" % sys.argv[0]
    sys.exit(1)

def createImage(data):
    zoom = 4
    rmin = -zoom/2
    rmax = zoom/2
    if zoom > 1:
        r = range(-zoom/2, zoom/2+1)
    else:
        r = [ 0 ]
    sz = int(data.size)
    sz4 = sz * zoom
    
    img = Image.new("RGBA", (sz4, sz4), (255,255,255,0))
    draw = ImageDraw.Draw(img)

    draw.ellipse((0, 0, sz4-zoom, sz4-zoom),
                     fill=data.circle_color, outline=None)
    for i in r:
        draw.ellipse((i, i, sz4-i-zoom, sz4-i-zoom),
                     fill=None, outline=data.border_color)

    font = ImageFont.truetype(FONT_PATH, int(sz4 * .75))
    tsx, tsy = draw.textsize(data.letter, font=font)

    ptx = (sz4 - tsx) / 2
    pty = (sz4 - tsy) / 2
    for i in r:
        draw.text((ptx + i, pty), data.letter, font=font, fill=data.letter_color)

    img = img.resize((sz, sz), Image.BICUBIC)
    img.save(data.dest_name, "PNG")
    print "Saved", data.dest_name

if __name__ == "__main__":
    main(sys.argv[1:])