PaletteOptimiser

Changeset

4:23cf22b7cfbf
2015-09-08 Paul Boddie raw files shortlog changelog graph Experimenting with three tone values. Added automatic image height sizing.
optimiser.py (file)
     1.1 --- a/optimiser.py	Tue Sep 08 21:29:00 2015 +0200
     1.2 +++ b/optimiser.py	Tue Sep 08 22:09:13 2015 +0200
     1.3 @@ -2,7 +2,6 @@
     1.4  
     1.5  from array import array
     1.6  from itertools import combinations
     1.7 -from random import randint
     1.8  import PIL.Image
     1.9  import sys
    1.10  
    1.11 @@ -35,25 +34,22 @@
    1.12  def match_darker(b, bases):
    1.13      return downward[b] in bases and downward[b]
    1.14  
    1.15 -def neutral(bases, light):
    1.16 -    l = ["W", "C", "Y", "M", "G", "R", "B", "_"]
    1.17 -    if not light:
    1.18 -        l.reverse()
    1.19 -    for b in l:
    1.20 -        if b in bases:
    1.21 +def fallback(bases):
    1.22 +    for b in by_frequency(bases):
    1.23 +        if b not in ["_", "W"]:
    1.24              return b
    1.25 -    return bases[randint(0, 3)]
    1.26 +    return by_frequency(bases)[0]
    1.27  
    1.28  tones = [
    1.29 -    "__", "_B", "BB", # 00x
    1.30 -    "_G", "_C", "BC", # 01x
    1.31 -    "GG", "GC", "CC", # 02x
    1.32 -    "_R", "_M", "BM", # 10x
    1.33 -    "_Y", "**", "WB", # 11x
    1.34 -    "GY", "WG", "WC", # 12x
    1.35 -    "RR", "RM", "MM", # 20x
    1.36 -    "RY", "WR", "WM", # 21x
    1.37 -    "YY", "WY", "WW", # 22x
    1.38 +    "___", "_BB", "BBB", # 00x
    1.39 +    "_GG", "_CC", "BCC", # 01x
    1.40 +    "GGG", "GCC", "CCC", # 02x
    1.41 +    "_RR", "_MM", "BMM", # 10x
    1.42 +    "_YY", "_*W", "BBW", # 11x
    1.43 +    "GYY", "GGW", "CCW", # 12x
    1.44 +    "RRR", "RMM", "MMM", # 20x
    1.45 +    "RYY", "RRW", "MMW", # 21x
    1.46 +    "YYY", "YYW", "WWW", # 22x
    1.47      ]
    1.48  
    1.49  colours = ["_", "R", "G", "Y", "B", "M", "C", "W"]
    1.50 @@ -84,10 +80,11 @@
    1.51  
    1.52  if __name__ == "__main__":
    1.53      width = 320
    1.54 -    height = 192
    1.55      input_filename, output_filename = sys.argv[1:3]
    1.56  
    1.57      im = PIL.Image.open(input_filename)
    1.58 +    w, h = im.size
    1.59 +    height = (width * h) / w
    1.60      im = im.resize((width, height))
    1.61  
    1.62      usage = []
    1.63 @@ -111,6 +108,8 @@
    1.64                  add(bu, t[0])
    1.65              if t[1] != "*":
    1.66                  add(bu, t[1])
    1.67 +            if t[2] != "*":
    1.68 +                add(bu, t[2])
    1.69              tr.append(t)
    1.70  
    1.71      chosen = []
    1.72 @@ -122,11 +121,12 @@
    1.73          best_missing = None
    1.74          best_map = None
    1.75          for bases in combinations(bu, 4):
    1.76 +            bases = dict([(base, bu[base]) for base in bases])
    1.77              count = 0
    1.78              missing = []
    1.79              tone_map = {}
    1.80              for tone, freq in u.items():
    1.81 -                base = match(light and tone[1] or tone[0], bases)
    1.82 +                base = match(tone[1], bases) or match(light and tone[2] or tone[0], bases)
    1.83                  if base:
    1.84                      tone_map[tone] = base
    1.85                      count += freq
    1.86 @@ -142,11 +142,10 @@
    1.87      output = []
    1.88  
    1.89      for row, (tr, ch) in enumerate(zip(toned, chosen)):
    1.90 -        light = row % 2
    1.91          o = []
    1.92          for column, t in enumerate(tr):
    1.93              best, bases, tone_map, missing = ch
    1.94 -            base = tone_map.get(t) or neutral(bases, light)
    1.95 +            base = tone_map.get(t) or fallback(bases)
    1.96              o.append(base)
    1.97              i = colours.index(base)
    1.98              im.putpixel((column, row), colour(i))