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