# HG changeset patch # User Paul Boddie # Date 1441716125 -7200 # Node ID 19ad565c6647cefe95712ee585c9848711e81b86 A palette optimiser tool converting images to primary and secondary colours. diff -r 000000000000 -r 19ad565c6647 optimiser.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/optimiser.py Tue Sep 08 14:42:05 2015 +0200 @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +from array import array +import PIL.Image +import sys + +def scale(v): + return (v + 64) / 128 + +def point(rgb): + return tuple(map(scale, rgb)) + +def index(p): + return p[0] * 9 + p[1] * 3 + p[2] + +def colour(i): + return (255 * (i % 2), 255 * ((i / 2) % 2), 255 * ((i / 4) % 2)) + +tones = [ + "__", "B_", "BB", # 00x + "G_", "C_", "CB", # 01x + "GG", "CG", "CC", # 02x + "R_", "M_", "MB", # 10x + "Y_", "W_", "WB", # 11x + "YG", "WG", "WC", # 12x + "RR", "MR", "MM", # 20x + "YR", "WR", "WM", # 21x + "YY", "WY", "WW", # 22x + ] + +colours = ["_", "R", "G", "Y", "B", "M", "C", "W"] + +if __name__ == "__main__": + + input_filename, output_filename = sys.argv[1:3] + + im = PIL.Image.open(input_filename) + im = im.resize((320, 256)) + + for row in range(0, 256): + for column in range(0, 320): + rgb = im.getpixel((column, row)) + p = point(rgb) + i = index(p) + t = tones[i] + c = t[row % 2] + i = colours.index(c) + im.putpixel((column, row), colour(i)) + + im.save(output_filename) + +# vim: tabstop=4 expandtab shiftwidth=4