PaletteOptimiser

Change of optimiser.py

0:19ad565c6647
optimiser.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/optimiser.py	Tue Sep 08 14:42:05 2015 +0200
     1.3 @@ -0,0 +1,52 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +from array import array
     1.7 +import PIL.Image
     1.8 +import sys
     1.9 +
    1.10 +def scale(v):
    1.11 +    return (v + 64) / 128
    1.12 +
    1.13 +def point(rgb):
    1.14 +    return tuple(map(scale, rgb))
    1.15 +
    1.16 +def index(p):
    1.17 +    return p[0] * 9 + p[1] * 3 + p[2]
    1.18 +
    1.19 +def colour(i):
    1.20 +    return (255 * (i % 2), 255 * ((i / 2) % 2), 255 * ((i / 4) % 2))
    1.21 +
    1.22 +tones = [
    1.23 +    "__", "B_", "BB", # 00x
    1.24 +    "G_", "C_", "CB", # 01x
    1.25 +    "GG", "CG", "CC", # 02x
    1.26 +    "R_", "M_", "MB", # 10x
    1.27 +    "Y_", "W_", "WB", # 11x
    1.28 +    "YG", "WG", "WC", # 12x
    1.29 +    "RR", "MR", "MM", # 20x
    1.30 +    "YR", "WR", "WM", # 21x
    1.31 +    "YY", "WY", "WW", # 22x
    1.32 +    ]
    1.33 +
    1.34 +colours = ["_", "R", "G", "Y", "B", "M", "C", "W"]
    1.35 +
    1.36 +if __name__ == "__main__":
    1.37 +
    1.38 +    input_filename, output_filename = sys.argv[1:3]
    1.39 +
    1.40 +    im = PIL.Image.open(input_filename)
    1.41 +    im = im.resize((320, 256))
    1.42 +
    1.43 +    for row in range(0, 256):
    1.44 +        for column in range(0, 320):
    1.45 +            rgb = im.getpixel((column, row))
    1.46 +            p = point(rgb)
    1.47 +            i = index(p)
    1.48 +            t = tones[i]
    1.49 +            c = t[row % 2]
    1.50 +            i = colours.index(c)
    1.51 +            im.putpixel((column, row), colour(i))
    1.52 +
    1.53 +    im.save(output_filename)
    1.54 +
    1.55 +# vim: tabstop=4 expandtab shiftwidth=4