PaletteOptimiser

Changeset

11:4e6369d60870
2015-09-09 Paul Boddie raw files shortlog changelog graph Corrected some colour entries, added weighting for preferred tones. Added support for rotating the input image.
optimiser.py (file)
     1.1 --- a/optimiser.py	Wed Sep 09 00:07:25 2015 +0200
     1.2 +++ b/optimiser.py	Wed Sep 09 00:30:58 2015 +0200
     1.3 @@ -35,20 +35,20 @@
     1.4      return by_frequency(bases)[0]
     1.5  
     1.6  tones = [
     1.7 -    "___", "_BB", "BBB", "BBB", # 00x
     1.8 -    "_GG", "_GC", "GGC", "GCC", # 01x
     1.9 -    "GGG", "GGC", "GCC", "GCC", # 02x
    1.10 +    "___", "_BB", "_BB", "BBB", # 00x
    1.11 +    "_GG", "__C", "_BC", "BCC", # 01x
    1.12 +    "_GG", "GGC", "BCC", "CCC", # 02x
    1.13      "GGG", "GCC", "CCC", "CCC", # 03x
    1.14      "_RR", "_MM", "MMB", "MBB", # 10x
    1.15      "_YY", "_**", "_*B", "BBW", # 11x
    1.16 -    "GYY", "GGW", "GCC", "CCW", # 12x
    1.17 +    "_GY", "GGC", "*CC", "CCW", # 12x
    1.18      "GGY", "GGG", "GCC", "CCW", # 13x
    1.19      "RRR", "RRM", "RMM", "MMM", # 20x
    1.20      "RYY", "RRW", "RMW", "MMW", # 21x
    1.21      "YYY", "YYW", "**W", "WWW", # 22x
    1.22      "YYY", "YYW", "YWW", "WWW", # 23x
    1.23 -    "RRR", "RMM", "RMM", "MMM", # 30x
    1.24 -    "RRY", "RRY", "RMM", "MMW", # 31x
    1.25 +    "RRR", "RMM", "RMM", "MMW", # 30x
    1.26 +    "RRY", "RRY", "RMW", "MMW", # 31x
    1.27      "YYY", "YYW", "YYW", "WWW", # 32x
    1.28      "YYY", "YYW", "YYW", "WWW", # 33x
    1.29      ]
    1.30 @@ -58,8 +58,11 @@
    1.31  if __name__ == "__main__":
    1.32      width = 320
    1.33      input_filename, output_filename = sys.argv[1:3]
    1.34 +    rotate = "-r" in sys.argv[3:]
    1.35  
    1.36      im = PIL.Image.open(input_filename)
    1.37 +    if rotate:
    1.38 +        im = im.rotate(270)
    1.39      w, h = im.size
    1.40      height = (width * h) / w
    1.41      im = im.resize((width, height))
    1.42 @@ -104,12 +107,17 @@
    1.43              missing = []
    1.44              tone_map = {}
    1.45              for tone, freq in u.items():
    1.46 -                base = match(tone[1], bases) or match(light and tone[2] or tone[0], bases)
    1.47 +                base = match(tone[1], bases)
    1.48                  if base:
    1.49                      tone_map[tone] = base
    1.50                      count += freq
    1.51                  else:
    1.52 -                    missing.append(tone)
    1.53 +                    base = match(light and tone[2] or tone[0], bases)
    1.54 +                    if base:
    1.55 +                        tone_map[tone] = base
    1.56 +                        count += freq / 2
    1.57 +                    else:
    1.58 +                        missing.append(tone)
    1.59              if count > best:
    1.60                  best_bases = bases
    1.61                  best_missing = missing