PaletteOptimiser

optimiser.py

0:19ad565c6647
2015-09-08 Paul Boddie A palette optimiser tool converting images to primary and secondary colours.
     1 #!/usr/bin/env python     2      3 from array import array     4 import PIL.Image     5 import sys     6      7 def scale(v):     8     return (v + 64) / 128     9     10 def point(rgb):    11     return tuple(map(scale, rgb))    12     13 def index(p):    14     return p[0] * 9 + p[1] * 3 + p[2]    15     16 def colour(i):    17     return (255 * (i % 2), 255 * ((i / 2) % 2), 255 * ((i / 4) % 2))    18     19 tones = [    20     "__", "B_", "BB", # 00x    21     "G_", "C_", "CB", # 01x    22     "GG", "CG", "CC", # 02x    23     "R_", "M_", "MB", # 10x    24     "Y_", "W_", "WB", # 11x    25     "YG", "WG", "WC", # 12x    26     "RR", "MR", "MM", # 20x    27     "YR", "WR", "WM", # 21x    28     "YY", "WY", "WW", # 22x    29     ]    30     31 colours = ["_", "R", "G", "Y", "B", "M", "C", "W"]    32     33 if __name__ == "__main__":    34     35     input_filename, output_filename = sys.argv[1:3]    36     37     im = PIL.Image.open(input_filename)    38     im = im.resize((320, 256))    39     40     for row in range(0, 256):    41         for column in range(0, 320):    42             rgb = im.getpixel((column, row))    43             p = point(rgb)    44             i = index(p)    45             t = tones[i]    46             c = t[row % 2]    47             i = colours.index(c)    48             im.putpixel((column, row), colour(i))    49     50     im.save(output_filename)    51     52 # vim: tabstop=4 expandtab shiftwidth=4