# HG changeset patch # User Paul Boddie # Date 1441753504 -7200 # Node ID 9369980667e7fed0fe93f0373062cf53e0893599 # Parent 19ad565c6647cefe95712ee585c9848711e81b86 Switched to the more advanced colour model. Added automatic height sizing and rotation support. diff -r 19ad565c6647 -r 9369980667e7 optimiser.py --- a/optimiser.py Tue Sep 08 14:42:05 2015 +0200 +++ b/optimiser.py Wed Sep 09 01:05:04 2015 +0200 @@ -5,48 +5,66 @@ import sys def scale(v): - return (v + 64) / 128 + return (v + 43) / 85 def point(rgb): return tuple(map(scale, rgb)) def index(p): - return p[0] * 9 + p[1] * 3 + p[2] + return p[0] * 16 + p[1] * 4 + 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 + "___", "_BB", "_BB", "BBB", # 00x + "_GG", "__C", "_BC", "BCC", # 01x + "_GG", "GGC", "BCC", "CCC", # 02x + "GGG", "GCC", "CCC", "CCC", # 03x + "_RR", "_MM", "MMB", "MBB", # 10x + "_YY", "_**", "_*B", "BBW", # 11x + "_GY", "GGC", "*CC", "CCW", # 12x + "GGY", "GGG", "GCC", "CCW", # 13x + "RRR", "RRM", "RMM", "MMM", # 20x + "RYY", "RRW", "RMW", "MMW", # 21x + "YYY", "YYW", "**W", "WWW", # 22x + "YYY", "YYW", "YWW", "WWW", # 23x + "RRR", "RMM", "RMM", "MMW", # 30x + "RRY", "RRY", "RMW", "MMW", # 31x + "YYY", "YYW", "YYW", "WWW", # 32x + "YYY", "YYW", "YYW", "WWW", # 33x ] colours = ["_", "R", "G", "Y", "B", "M", "C", "W"] if __name__ == "__main__": - + width = 320 input_filename, output_filename = sys.argv[1:3] + scaled_filename = (sys.argv[3:] + [None])[0] + rotate = "-r" in sys.argv[3:] im = PIL.Image.open(input_filename) - im = im.resize((320, 256)) + if rotate: + im = im.rotate(270) + w, h = im.size + height = (width * h) / w + im = im.resize((width, height)) - for row in range(0, 256): - for column in range(0, 320): + for row in range(0, height): + for column in range(0, width): rgb = im.getpixel((column, row)) p = point(rgb) i = index(p) t = tones[i] - c = t[row % 2] + c = t[1] != "*" and t[1] or (row % 2) and t[2] != "*" and t[2] or t[0] != "*" and t[0] or t[2] i = colours.index(c) im.putpixel((column, row), colour(i)) im.save(output_filename) + if scaled_filename: + im = im.resize((width / 2, height), PIL.Image.NEAREST) + im = im.resize((width, height), PIL.Image.NEAREST) + im.save(scaled_filename) + # vim: tabstop=4 expandtab shiftwidth=4