paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | from array import array |
paul@1 | 4 | from itertools import combinations |
paul@0 | 5 | import PIL.Image |
paul@0 | 6 | import sys |
paul@0 | 7 | |
paul@0 | 8 | def scale(v): |
paul@0 | 9 | return (v + 64) / 128 |
paul@0 | 10 | |
paul@0 | 11 | def point(rgb): |
paul@0 | 12 | return tuple(map(scale, rgb)) |
paul@0 | 13 | |
paul@0 | 14 | def index(p): |
paul@0 | 15 | return p[0] * 9 + p[1] * 3 + p[2] |
paul@0 | 16 | |
paul@0 | 17 | def colour(i): |
paul@0 | 18 | return (255 * (i % 2), 255 * ((i / 2) % 2), 255 * ((i / 4) % 2)) |
paul@0 | 19 | |
paul@1 | 20 | def add(d, v): |
paul@1 | 21 | d[v] = (d.has_key(v) and d[v] or 0) + 1 |
paul@1 | 22 | |
paul@1 | 23 | def by_frequency(d): |
paul@1 | 24 | l = [(f, t) for (t, f) in d.items()] |
paul@1 | 25 | l.sort(reverse=True) |
paul@1 | 26 | return [i[1] for i in l] |
paul@1 | 27 | |
paul@1 | 28 | def match(b, bases): |
paul@1 | 29 | return b in bases and b |
paul@1 | 30 | |
paul@1 | 31 | def match_lighter(b, bases): |
paul@1 | 32 | return upward[b] in bases and upward[b] |
paul@1 | 33 | |
paul@1 | 34 | def match_darker(b, bases): |
paul@1 | 35 | return downward[b] in bases and downward[b] |
paul@1 | 36 | |
paul@4 | 37 | def fallback(bases): |
paul@4 | 38 | for b in by_frequency(bases): |
paul@4 | 39 | if b not in ["_", "W"]: |
paul@1 | 40 | return b |
paul@4 | 41 | return by_frequency(bases)[0] |
paul@1 | 42 | |
paul@0 | 43 | tones = [ |
paul@4 | 44 | "___", "_BB", "BBB", # 00x |
paul@4 | 45 | "_GG", "_CC", "BCC", # 01x |
paul@4 | 46 | "GGG", "GCC", "CCC", # 02x |
paul@4 | 47 | "_RR", "_MM", "BMM", # 10x |
paul@4 | 48 | "_YY", "_*W", "BBW", # 11x |
paul@4 | 49 | "GYY", "GGW", "CCW", # 12x |
paul@4 | 50 | "RRR", "RMM", "MMM", # 20x |
paul@4 | 51 | "RYY", "RRW", "MMW", # 21x |
paul@4 | 52 | "YYY", "YYW", "WWW", # 22x |
paul@0 | 53 | ] |
paul@0 | 54 | |
paul@0 | 55 | colours = ["_", "R", "G", "Y", "B", "M", "C", "W"] |
paul@0 | 56 | |
paul@1 | 57 | upward = { |
paul@1 | 58 | "_" : ["R", "G", "B"], |
paul@1 | 59 | "R" : ["Y", "M"], |
paul@1 | 60 | "G" : ["Y", "C"], |
paul@1 | 61 | "B" : ["C", "M"], |
paul@1 | 62 | "Y" : ["W"], |
paul@1 | 63 | "C" : ["W"], |
paul@1 | 64 | "M" : ["W"], |
paul@1 | 65 | "W" : ["W"], |
paul@1 | 66 | "*" : ["W", "Y", "C", "M"], |
paul@1 | 67 | } |
paul@1 | 68 | |
paul@1 | 69 | downward = { |
paul@1 | 70 | "_" : ["_"], |
paul@1 | 71 | "R" : ["_"], |
paul@1 | 72 | "G" : ["_"], |
paul@1 | 73 | "B" : ["_"], |
paul@1 | 74 | "Y" : ["R", "G"], |
paul@1 | 75 | "C" : ["G", "B"], |
paul@1 | 76 | "M" : ["R", "B"], |
paul@1 | 77 | "W" : ["Y", "C", "M"], |
paul@1 | 78 | "*" : ["R", "G", "B", "_"], |
paul@1 | 79 | } |
paul@1 | 80 | |
paul@0 | 81 | if __name__ == "__main__": |
paul@1 | 82 | width = 320 |
paul@0 | 83 | input_filename, output_filename = sys.argv[1:3] |
paul@0 | 84 | |
paul@0 | 85 | im = PIL.Image.open(input_filename) |
paul@4 | 86 | w, h = im.size |
paul@4 | 87 | height = (width * h) / w |
paul@1 | 88 | im = im.resize((width, height)) |
paul@1 | 89 | |
paul@1 | 90 | usage = [] |
paul@1 | 91 | base_usage = [] |
paul@1 | 92 | toned = [] |
paul@0 | 93 | |
paul@1 | 94 | for row in range(0, height): |
paul@1 | 95 | u = {} |
paul@1 | 96 | usage.append(u) |
paul@1 | 97 | bu = {} |
paul@1 | 98 | base_usage.append(bu) |
paul@1 | 99 | tr = [] |
paul@1 | 100 | toned.append(tr) |
paul@1 | 101 | for column in range(0, width): |
paul@0 | 102 | rgb = im.getpixel((column, row)) |
paul@0 | 103 | p = point(rgb) |
paul@0 | 104 | i = index(p) |
paul@0 | 105 | t = tones[i] |
paul@1 | 106 | add(u, t) |
paul@1 | 107 | if t[0] != "*": |
paul@1 | 108 | add(bu, t[0]) |
paul@1 | 109 | if t[1] != "*": |
paul@1 | 110 | add(bu, t[1]) |
paul@4 | 111 | if t[2] != "*": |
paul@4 | 112 | add(bu, t[2]) |
paul@1 | 113 | tr.append(t) |
paul@1 | 114 | |
paul@1 | 115 | chosen = [] |
paul@1 | 116 | |
paul@2 | 117 | for row, (u, bu) in enumerate(zip(usage, base_usage)): |
paul@2 | 118 | light = row % 2 |
paul@1 | 119 | best = 0 |
paul@1 | 120 | best_bases = None |
paul@1 | 121 | best_missing = None |
paul@1 | 122 | best_map = None |
paul@1 | 123 | for bases in combinations(bu, 4): |
paul@4 | 124 | bases = dict([(base, bu[base]) for base in bases]) |
paul@1 | 125 | count = 0 |
paul@1 | 126 | missing = [] |
paul@1 | 127 | tone_map = {} |
paul@1 | 128 | for tone, freq in u.items(): |
paul@4 | 129 | base = match(tone[1], bases) or match(light and tone[2] or tone[0], bases) |
paul@1 | 130 | if base: |
paul@1 | 131 | tone_map[tone] = base |
paul@1 | 132 | count += freq |
paul@1 | 133 | else: |
paul@1 | 134 | missing.append(tone) |
paul@1 | 135 | if count > best: |
paul@1 | 136 | best_bases = bases |
paul@1 | 137 | best_missing = missing |
paul@1 | 138 | best_map = tone_map |
paul@1 | 139 | best = count |
paul@1 | 140 | chosen.append((best, best_bases or bases, best_map or tone_map, best_missing or missing)) |
paul@1 | 141 | |
paul@1 | 142 | output = [] |
paul@1 | 143 | |
paul@1 | 144 | for row, (tr, ch) in enumerate(zip(toned, chosen)): |
paul@1 | 145 | o = [] |
paul@1 | 146 | for column, t in enumerate(tr): |
paul@1 | 147 | best, bases, tone_map, missing = ch |
paul@4 | 148 | base = tone_map.get(t) or fallback(bases) |
paul@1 | 149 | o.append(base) |
paul@3 | 150 | i = colours.index(base) |
paul@0 | 151 | im.putpixel((column, row), colour(i)) |
paul@0 | 152 | |
paul@1 | 153 | output.append("".join(o)) |
paul@1 | 154 | |
paul@0 | 155 | im.save(output_filename) |
paul@0 | 156 | |
paul@0 | 157 | # vim: tabstop=4 expandtab shiftwidth=4 |