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