source code of /small-scripts/favicon.py
Last modified | |
Lines | 44 |
Parent directory Download CGIread sitemap Main page
Quick links: main
#!/usr/bin/python
def main(in_name, out_name, is_text=1):
def base16(n):
return ''.join(['0123456789abcdef'[(n/16**i)%16] for i in range(5,-1,-1)])
fi = open(in_name)
s = fi.read(3072)
fi.close()
img = [[None for a in range(32)] for b in range(32)]
for i in range(0,3072,3):
img[i/96][(i/3)%32] = (ord(s[i])*2<<16 | ord(s[i+1])<<8 | ord(s[i+2])) << is_text
colors = []
for line in img:
for pix in line:
if pix not in colors:
colors.append(pix)
fo = open(out_name, 'w')
fo.write('''#define FAVICON_format 1
#define FAVICON_width 32
#define FAVICON_height 32
''')
fo.write('#define FAVICON_ncolors {}\n'.format(len(colors)))
fo.write('''#define FAVICON_chars_per_pixel 6
static char *FAVICON_colors[] = {
''')
for col in colors[:-1]:
fo.write('"{0}", "#{0}",\n'.format(base16(col)))
fo.write('"{0}", "#{0}"\n'.format(base16(colors[-1])))
fo.write('''};
static char *FAVICON_pixels[] = {
''')
for line in img[:-1]:
fo.write('"{0}",\n'.format(''.join(map(base16, line))))
fo.write('"{0}"\n'.format(''.join(map(base16, img[-1]))))
fo.write('};\n')
fo.close()