PaletteOptimiser

Change of optimiser.py

6:cc44040363e1
optimiser.py
     1.1 --- a/optimiser.py	Tue Sep 08 22:14:55 2015 +0200
     1.2 +++ b/optimiser.py	Tue Sep 08 22:22:45 2015 +0200
     1.3 @@ -35,14 +35,11 @@
     1.4  def match_darker(b, bases):
     1.5      return downward[b] in bases and downward[b]
     1.6  
     1.7 -def neutral(bases, light):
     1.8 -    l = ["W", "C", "Y", "M", "G", "R", "B", "_"]
     1.9 -    if not light:
    1.10 -        l.reverse()
    1.11 -    for b in l:
    1.12 -        if b in bases:
    1.13 +def fallback(bases):
    1.14 +    for b in by_frequency(bases):
    1.15 +        if b not in ["_", "W"]:
    1.16              return b
    1.17 -    return bases[randint(0, 3)]
    1.18 +    return by_frequency(bases)[0]
    1.19  
    1.20  tones = [
    1.21      "__", "_B", "BB", # 00x
    1.22 @@ -58,30 +55,6 @@
    1.23  
    1.24  colours = ["_", "R", "G", "Y", "B", "M", "C", "W"]
    1.25  
    1.26 -upward = {
    1.27 -    "_" : ["R", "G", "B"],
    1.28 -    "R" : ["Y", "M"],
    1.29 -    "G" : ["Y", "C"],
    1.30 -    "B" : ["C", "M"],
    1.31 -    "Y" : ["W"],
    1.32 -    "C" : ["W"],
    1.33 -    "M" : ["W"],
    1.34 -    "W" : ["W"],
    1.35 -    "*" : ["W", "Y", "C", "M"],
    1.36 -    }
    1.37 -
    1.38 -downward = {
    1.39 -    "_" : ["_"],
    1.40 -    "R" : ["_"],
    1.41 -    "G" : ["_"],
    1.42 -    "B" : ["_"],
    1.43 -    "Y" : ["R", "G"],
    1.44 -    "C" : ["G", "B"],
    1.45 -    "M" : ["R", "B"],
    1.46 -    "W" : ["Y", "C", "M"],
    1.47 -    "*" : ["R", "G", "B", "_"],
    1.48 -    }
    1.49 -
    1.50  if __name__ == "__main__":
    1.51      width = 320
    1.52      input_filename, output_filename = sys.argv[1:3]
    1.53 @@ -117,17 +90,17 @@
    1.54      chosen = []
    1.55  
    1.56      for row, (u, bu) in enumerate(zip(usage, base_usage)):
    1.57 -        light = row % 2
    1.58          best = 0
    1.59          best_bases = None
    1.60          best_missing = None
    1.61          best_map = None
    1.62          for bases in combinations(bu, 4):
    1.63 +            bases = dict([(base, bu[base]) for base in bases])
    1.64              count = 0
    1.65              missing = []
    1.66              tone_map = {}
    1.67              for tone, freq in u.items():
    1.68 -                base = match(light and tone[1] or tone[0], bases)
    1.69 +                base = match(tone[1], bases) or match(tone[0], bases)
    1.70                  if base:
    1.71                      tone_map[tone] = base
    1.72                      count += freq
    1.73 @@ -143,11 +116,10 @@
    1.74      output = []
    1.75  
    1.76      for row, (tr, ch) in enumerate(zip(toned, chosen)):
    1.77 -        light = row % 2
    1.78          o = []
    1.79          for column, t in enumerate(tr):
    1.80              best, bases, tone_map, missing = ch
    1.81 -            base = tone_map.get(t) or neutral(bases, light)
    1.82 +            base = tone_map.get(t) or fallback(bases)
    1.83              o.append(base)
    1.84              i = colours.index(base)
    1.85              im.putpixel((column, row), colour(i))