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@9 | 9 | return (v + 43) / 85 |
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@9 | 15 | return p[0] * 16 + p[1] * 4 + 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@4 | 31 | def fallback(bases): |
paul@4 | 32 | for b in by_frequency(bases): |
paul@4 | 33 | if b not in ["_", "W"]: |
paul@1 | 34 | return b |
paul@4 | 35 | return by_frequency(bases)[0] |
paul@1 | 36 | |
paul@0 | 37 | tones = [ |
paul@9 | 38 | "___", "_BB", "BBB", "BBB", # 00x |
paul@9 | 39 | "_GG", "_GC", "GGC", "GCC", # 01x |
paul@9 | 40 | "GGG", "GGC", "GCC", "GCC", # 02x |
paul@9 | 41 | "GGG", "GCC", "CCC", "CCC", # 03x |
paul@9 | 42 | "_RR", "_MM", "MMB", "MBB", # 10x |
paul@9 | 43 | "_YY", "_**", "_*B", "BBW", # 11x |
paul@9 | 44 | "GYY", "GGW", "GCC", "CCW", # 12x |
paul@9 | 45 | "GGY", "GGG", "GCC", "CCW", # 13x |
paul@9 | 46 | "RRR", "RRM", "RMM", "MMM", # 20x |
paul@9 | 47 | "RYY", "RRW", "RMW", "MMW", # 21x |
paul@9 | 48 | "YYY", "YYW", "**W", "WWW", # 22x |
paul@9 | 49 | "YYY", "YYW", "YWW", "WWW", # 23x |
paul@9 | 50 | "RRR", "RMM", "RMM", "MMM", # 30x |
paul@9 | 51 | "RRY", "RRY", "RMM", "MMW", # 31x |
paul@9 | 52 | "YYY", "YYW", "YYW", "WWW", # 32x |
paul@9 | 53 | "YYY", "YYW", "YYW", "WWW", # 33x |
paul@0 | 54 | ] |
paul@0 | 55 | |
paul@0 | 56 | colours = ["_", "R", "G", "Y", "B", "M", "C", "W"] |
paul@0 | 57 | |
paul@0 | 58 | if __name__ == "__main__": |
paul@1 | 59 | width = 320 |
paul@0 | 60 | input_filename, output_filename = sys.argv[1:3] |
paul@0 | 61 | |
paul@0 | 62 | im = PIL.Image.open(input_filename) |
paul@4 | 63 | w, h = im.size |
paul@4 | 64 | height = (width * h) / w |
paul@1 | 65 | im = im.resize((width, height)) |
paul@1 | 66 | |
paul@1 | 67 | usage = [] |
paul@1 | 68 | base_usage = [] |
paul@1 | 69 | toned = [] |
paul@0 | 70 | |
paul@1 | 71 | for row in range(0, height): |
paul@1 | 72 | u = {} |
paul@1 | 73 | usage.append(u) |
paul@1 | 74 | bu = {} |
paul@1 | 75 | base_usage.append(bu) |
paul@1 | 76 | tr = [] |
paul@1 | 77 | toned.append(tr) |
paul@1 | 78 | for column in range(0, width): |
paul@0 | 79 | rgb = im.getpixel((column, row)) |
paul@0 | 80 | p = point(rgb) |
paul@0 | 81 | i = index(p) |
paul@0 | 82 | t = tones[i] |
paul@1 | 83 | add(u, t) |
paul@1 | 84 | if t[0] != "*": |
paul@1 | 85 | add(bu, t[0]) |
paul@1 | 86 | if t[1] != "*": |
paul@1 | 87 | add(bu, t[1]) |
paul@4 | 88 | if t[2] != "*": |
paul@4 | 89 | add(bu, t[2]) |
paul@1 | 90 | tr.append(t) |
paul@1 | 91 | |
paul@1 | 92 | chosen = [] |
paul@1 | 93 | |
paul@2 | 94 | for row, (u, bu) in enumerate(zip(usage, base_usage)): |
paul@2 | 95 | light = row % 2 |
paul@1 | 96 | best = 0 |
paul@1 | 97 | best_bases = None |
paul@1 | 98 | best_missing = None |
paul@1 | 99 | best_map = None |
paul@10 | 100 | |
paul@10 | 101 | for bases in combinations(bu, min(len(bu), 4)): |
paul@4 | 102 | bases = dict([(base, bu[base]) for base in bases]) |
paul@1 | 103 | count = 0 |
paul@1 | 104 | missing = [] |
paul@1 | 105 | tone_map = {} |
paul@1 | 106 | for tone, freq in u.items(): |
paul@4 | 107 | base = match(tone[1], bases) or match(light and tone[2] or tone[0], bases) |
paul@1 | 108 | if base: |
paul@1 | 109 | tone_map[tone] = base |
paul@1 | 110 | count += freq |
paul@1 | 111 | else: |
paul@1 | 112 | missing.append(tone) |
paul@1 | 113 | if count > best: |
paul@1 | 114 | best_bases = bases |
paul@1 | 115 | best_missing = missing |
paul@1 | 116 | best_map = tone_map |
paul@1 | 117 | best = count |
paul@10 | 118 | |
paul@1 | 119 | chosen.append((best, best_bases or bases, best_map or tone_map, best_missing or missing)) |
paul@1 | 120 | |
paul@1 | 121 | output = [] |
paul@1 | 122 | |
paul@1 | 123 | for row, (tr, ch) in enumerate(zip(toned, chosen)): |
paul@1 | 124 | o = [] |
paul@1 | 125 | for column, t in enumerate(tr): |
paul@1 | 126 | best, bases, tone_map, missing = ch |
paul@4 | 127 | base = tone_map.get(t) or fallback(bases) |
paul@1 | 128 | o.append(base) |
paul@3 | 129 | i = colours.index(base) |
paul@0 | 130 | im.putpixel((column, row), colour(i)) |
paul@0 | 131 | |
paul@1 | 132 | output.append("".join(o)) |
paul@1 | 133 | |
paul@0 | 134 | im.save(output_filename) |
paul@0 | 135 | |
paul@0 | 136 | # vim: tabstop=4 expandtab shiftwidth=4 |