source code of /small-scripts/favicon.py

Last modified
Lines 44

Parent directory Download CGIread sitemap Main page

Quick links: main

  1. #!/usr/bin/python
  2. def main(in_name, out_name, is_text=1):
  3.     def base16(n):
  4.         return ''.join(['0123456789abcdef'[(n/16**i)%16] for i in range(5,-1,-1)])
  5.     fi = open(in_name)
  6.     s = fi.read(3072)
  7.     fi.close()
  8.     img = [[None for a in range(32)] for b in range(32)]
  9.     for i in range(0,3072,3):
  10.         img[i/96][(i/3)%32] = (ord(s[i])*2<<16 | ord(s[i+1])<<8 | ord(s[i+2])) << is_text
  11.     
  12.     colors = []
  13.     for line in img:
  14.         for pix in line:
  15.             if pix not in colors:
  16.                 colors.append(pix)
  17.     fo = open(out_name, 'w')
  18.     fo.write('''#define FAVICON_format 1
  19. #define FAVICON_width 32
  20. #define FAVICON_height 32
  21. ''')
  22.     fo.write('#define FAVICON_ncolors {}\n'.format(len(colors)))
  23.     fo.write('''#define FAVICON_chars_per_pixel 6
  24. static char *FAVICON_colors[] = {
  25. ''')
  26.     for col in colors[:-1]:
  27.         fo.write('"{0}", "#{0}",\n'.format(base16(col)))
  28.     fo.write('"{0}", "#{0}"\n'.format(base16(colors[-1])))
  29.     fo.write('''};
  30. static char *FAVICON_pixels[] = {
  31. ''')
  32.     for line in img[:-1]:
  33.         fo.write('"{0}",\n'.format(''.join(map(base16, line))))
  34.     fo.write('"{0}"\n'.format(''.join(map(base16, img[-1]))))
  35.     fo.write('};\n')
  36.     fo.close()