PaletteOptimiser

Changeset

14:9369980667e7
2015-09-09 Paul Boddie raw files shortlog changelog graph Switched to the more advanced colour model. Added automatic height sizing and rotation support.
optimiser.py (file)
     1.1 --- a/optimiser.py	Tue Sep 08 14:42:05 2015 +0200
     1.2 +++ b/optimiser.py	Wed Sep 09 01:05:04 2015 +0200
     1.3 @@ -5,48 +5,66 @@
     1.4  import sys
     1.5  
     1.6  def scale(v):
     1.7 -    return (v + 64) / 128
     1.8 +    return (v + 43) / 85
     1.9  
    1.10  def point(rgb):
    1.11      return tuple(map(scale, rgb))
    1.12  
    1.13  def index(p):
    1.14 -    return p[0] * 9 + p[1] * 3 + p[2]
    1.15 +    return p[0] * 16 + p[1] * 4 + p[2]
    1.16  
    1.17  def colour(i):
    1.18      return (255 * (i % 2), 255 * ((i / 2) % 2), 255 * ((i / 4) % 2))
    1.19  
    1.20  tones = [
    1.21 -    "__", "B_", "BB", # 00x
    1.22 -    "G_", "C_", "CB", # 01x
    1.23 -    "GG", "CG", "CC", # 02x
    1.24 -    "R_", "M_", "MB", # 10x
    1.25 -    "Y_", "W_", "WB", # 11x
    1.26 -    "YG", "WG", "WC", # 12x
    1.27 -    "RR", "MR", "MM", # 20x
    1.28 -    "YR", "WR", "WM", # 21x
    1.29 -    "YY", "WY", "WW", # 22x
    1.30 +    "___", "_BB", "_BB", "BBB", # 00x
    1.31 +    "_GG", "__C", "_BC", "BCC", # 01x
    1.32 +    "_GG", "GGC", "BCC", "CCC", # 02x
    1.33 +    "GGG", "GCC", "CCC", "CCC", # 03x
    1.34 +    "_RR", "_MM", "MMB", "MBB", # 10x
    1.35 +    "_YY", "_**", "_*B", "BBW", # 11x
    1.36 +    "_GY", "GGC", "*CC", "CCW", # 12x
    1.37 +    "GGY", "GGG", "GCC", "CCW", # 13x
    1.38 +    "RRR", "RRM", "RMM", "MMM", # 20x
    1.39 +    "RYY", "RRW", "RMW", "MMW", # 21x
    1.40 +    "YYY", "YYW", "**W", "WWW", # 22x
    1.41 +    "YYY", "YYW", "YWW", "WWW", # 23x
    1.42 +    "RRR", "RMM", "RMM", "MMW", # 30x
    1.43 +    "RRY", "RRY", "RMW", "MMW", # 31x
    1.44 +    "YYY", "YYW", "YYW", "WWW", # 32x
    1.45 +    "YYY", "YYW", "YYW", "WWW", # 33x
    1.46      ]
    1.47  
    1.48  colours = ["_", "R", "G", "Y", "B", "M", "C", "W"]
    1.49  
    1.50  if __name__ == "__main__":
    1.51 -
    1.52 +    width = 320
    1.53      input_filename, output_filename = sys.argv[1:3]
    1.54 +    scaled_filename = (sys.argv[3:] + [None])[0]
    1.55 +    rotate = "-r" in sys.argv[3:]
    1.56  
    1.57      im = PIL.Image.open(input_filename)
    1.58 -    im = im.resize((320, 256))
    1.59 +    if rotate:
    1.60 +        im = im.rotate(270)
    1.61 +    w, h = im.size
    1.62 +    height = (width * h) / w
    1.63 +    im = im.resize((width, height))
    1.64  
    1.65 -    for row in range(0, 256):
    1.66 -        for column in range(0, 320):
    1.67 +    for row in range(0, height):
    1.68 +        for column in range(0, width):
    1.69              rgb = im.getpixel((column, row))
    1.70              p = point(rgb)
    1.71              i = index(p)
    1.72              t = tones[i]
    1.73 -            c = t[row % 2]
    1.74 +            c = t[1] != "*" and t[1] or (row % 2) and t[2] != "*" and t[2] or t[0] != "*" and t[0] or t[2]
    1.75              i = colours.index(c)
    1.76              im.putpixel((column, row), colour(i))
    1.77  
    1.78      im.save(output_filename)
    1.79  
    1.80 +    if scaled_filename:
    1.81 +        im = im.resize((width / 2, height), PIL.Image.NEAREST)
    1.82 +        im = im.resize((width, height), PIL.Image.NEAREST)
    1.83 +        im.save(scaled_filename)
    1.84 +
    1.85  # vim: tabstop=4 expandtab shiftwidth=4