paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@28 | 3 | from random import random |
paul@32 | 4 | from os.path import splitext |
paul@12 | 5 | import EXIF |
paul@0 | 6 | import PIL.Image |
paul@32 | 7 | import itertools |
paul@20 | 8 | import math |
paul@0 | 9 | import sys |
paul@0 | 10 | |
paul@20 | 11 | corners = [ |
paul@20 | 12 | (0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0), |
paul@20 | 13 | (0, 0, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255) |
paul@0 | 14 | ] |
paul@0 | 15 | |
paul@20 | 16 | def distance(rgb1, rgb2): |
paul@20 | 17 | r1, g1, b1 = rgb1 |
paul@20 | 18 | r2, g2, b2 = rgb2 |
paul@20 | 19 | return math.sqrt(pow(r1 - r2, 2) + pow(g1 - g2, 2) + pow(b1 - b2, 2)) |
paul@20 | 20 | |
paul@34 | 21 | def nearest(rgb, values): |
paul@34 | 22 | l = [(distance(rgb, value), value) for value in values] |
paul@34 | 23 | l.sort() |
paul@34 | 24 | return l[0][1] |
paul@34 | 25 | |
paul@34 | 26 | def scale(rgb): |
paul@34 | 27 | return tuple(map(lambda x: x / 255.0, rgb)) |
paul@34 | 28 | |
paul@34 | 29 | def invert(srgb): |
paul@34 | 30 | return tuple(map(lambda x: 1.0 - x, srgb)) |
paul@34 | 31 | |
paul@34 | 32 | def combination(rgb): |
paul@34 | 33 | rgb = scale(rgb) |
paul@34 | 34 | rgbi = invert(rgb) |
paul@34 | 35 | pairs = zip(rgbi, rgb) |
paul@34 | 36 | d = [] |
paul@34 | 37 | for corner in corners: |
paul@34 | 38 | rs, gs, bs = scale(corner) |
paul@34 | 39 | d.append((pairs[0][int(rs)] * pairs[1][int(gs)] * pairs[2][int(bs)], corner)) |
paul@34 | 40 | return d |
paul@20 | 41 | |
paul@34 | 42 | def combine(d): |
paul@34 | 43 | out = [0, 0, 0] |
paul@34 | 44 | for v, rgb in d: |
paul@34 | 45 | out[0] += v * rgb[0] |
paul@34 | 46 | out[1] += v * rgb[1] |
paul@34 | 47 | out[2] += v * rgb[2] |
paul@34 | 48 | return out |
paul@34 | 49 | |
paul@34 | 50 | """ |
paul@34 | 51 | r(R - K) |
paul@34 | 52 | r(Y - G) |
paul@34 | 53 | r(M - B) |
paul@34 | 54 | r(W - C) |
paul@34 | 55 | g(G - K) |
paul@34 | 56 | g(Y - R) |
paul@34 | 57 | g(C - B) |
paul@34 | 58 | g(W - M) |
paul@34 | 59 | b(B - K) |
paul@34 | 60 | b(M - R) |
paul@34 | 61 | b(C - G) |
paul@34 | 62 | b(W - Y) |
paul@34 | 63 | |
paul@34 | 64 | b(g(r(W - C) - r(M - B)) - g(r(Y - G) - r(R - K))) |
paul@34 | 65 | b(r(g(W - M) - g(C - B)) - r(g(Y - R) - g(G - K))) |
paul@34 | 66 | |
paul@34 | 67 | b(g(rW + riC) + gi(rM + riB)) + bi(g(rY + riG) + gi(rR + riK)) |
paul@34 | 68 | b(r(gW + giM) + ri(gC + giB)) + bi(r(gY + giR) + ri(gG + giK)) |
paul@34 | 69 | |
paul@34 | 70 | W(b.g.r) + C(b.g.ri) + M(b.gi.r) + B(b.g.ri) + Y(bi.g.r) + G(bi.g.ri) + R(bi.gi.r) + K(bi.gi.ri) |
paul@34 | 71 | ... |
paul@34 | 72 | """ |
paul@34 | 73 | |
paul@34 | 74 | def pattern(rgb): |
paul@34 | 75 | l = combination(rgb) |
paul@32 | 76 | l.sort(reverse=True) |
paul@20 | 77 | return l |
paul@19 | 78 | |
paul@34 | 79 | def get_value(rgb): |
paul@32 | 80 | choose = random() |
paul@32 | 81 | threshold = 0 |
paul@34 | 82 | for f, c in pattern(rgb): |
paul@32 | 83 | threshold += f |
paul@32 | 84 | if choose < threshold: |
paul@32 | 85 | return c |
paul@32 | 86 | return c |
paul@0 | 87 | |
paul@21 | 88 | def sign(x): |
paul@21 | 89 | return x >= 0 and 1 or -1 |
paul@21 | 90 | |
paul@22 | 91 | def saturate_rgb(rgb, exp): |
paul@22 | 92 | return tuple([saturate_value(x, exp) for x in rgb]) |
paul@21 | 93 | |
paul@22 | 94 | def saturate_value(x, exp): |
paul@24 | 95 | return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp)) |
paul@21 | 96 | |
paul@33 | 97 | def replace(value, values): |
paul@33 | 98 | if value not in values: |
paul@33 | 99 | for i, v in list(enumerate(values))[::-1]: |
paul@33 | 100 | if v != value: |
paul@33 | 101 | values[i] = value |
paul@33 | 102 | return |
paul@33 | 103 | |
paul@20 | 104 | def test(): |
paul@20 | 105 | size = 512 |
paul@20 | 106 | for r in (0, 63, 127, 191, 255): |
paul@20 | 107 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 108 | for g in range(0, size): |
paul@20 | 109 | for b in range(0, size): |
paul@28 | 110 | value = get_value((r, (g * 256) / size, (b * 256 / size))) |
paul@20 | 111 | im.putpixel((g, b), value) |
paul@20 | 112 | im.save("rgb%d.png" % r) |
paul@20 | 113 | |
paul@20 | 114 | def test_flat(rgb): |
paul@20 | 115 | size = 64 |
paul@20 | 116 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 117 | for y in range(0, size): |
paul@20 | 118 | for x in range(0, size): |
paul@28 | 119 | im.putpixel((x, y), get_value(rgb)) |
paul@20 | 120 | im.save("rgb%02d%02d%02d.png" % rgb) |
paul@20 | 121 | |
paul@20 | 122 | def rotate_and_scale(im, width, height, rotate): |
paul@19 | 123 | if rotate or x and x["Image Orientation"].values == [6L]: |
paul@11 | 124 | im = im.rotate(270) |
paul@19 | 125 | |
paul@4 | 126 | w, h = im.size |
paul@19 | 127 | if w > h: |
paul@19 | 128 | height = (width * h) / w |
paul@19 | 129 | else: |
paul@19 | 130 | width = (height * w) / h |
paul@19 | 131 | |
paul@20 | 132 | return im.resize((width, height)) |
paul@1 | 133 | |
paul@20 | 134 | if __name__ == "__main__": |
paul@20 | 135 | if "--test" in sys.argv: |
paul@20 | 136 | test() |
paul@20 | 137 | sys.exit(0) |
paul@20 | 138 | elif "--test-flat" in sys.argv: |
paul@20 | 139 | test_flat((120, 40, 60)) |
paul@20 | 140 | sys.exit(0) |
paul@0 | 141 | |
paul@20 | 142 | width = 320 |
paul@20 | 143 | height = 256 |
paul@20 | 144 | |
paul@20 | 145 | input_filename, output_filename = sys.argv[1:3] |
paul@26 | 146 | basename, ext = splitext(output_filename) |
paul@26 | 147 | |
paul@20 | 148 | rotate = "-r" in sys.argv[3:] |
paul@23 | 149 | saturate = sys.argv[3:].count("-s") |
paul@23 | 150 | desaturate = sys.argv[3:].count("-d") |
paul@1 | 151 | |
paul@20 | 152 | x = EXIF.process_file(open(input_filename)) |
paul@32 | 153 | im = PIL.Image.open(input_filename).convert("RGB") |
paul@20 | 154 | im = rotate_and_scale(im, width, height, rotate) |
paul@1 | 155 | |
paul@20 | 156 | width, height = im.size |
paul@20 | 157 | |
paul@20 | 158 | colours = [] |
paul@10 | 159 | |
paul@20 | 160 | for y in range(0, height): |
paul@20 | 161 | c = {} |
paul@20 | 162 | for x in range(0, width): |
paul@20 | 163 | rgb = im.getpixel((x, y)) |
paul@24 | 164 | |
paul@24 | 165 | # Saturate if requested. |
paul@24 | 166 | |
paul@22 | 167 | if saturate or desaturate: |
paul@23 | 168 | rgb = saturate_rgb(rgb, saturate and math.pow(0.5, saturate) or math.pow(2, desaturate)) |
paul@24 | 169 | im.putpixel((x, y), rgb) |
paul@24 | 170 | |
paul@32 | 171 | # Sum the colour probabilities. |
paul@24 | 172 | |
paul@34 | 173 | for f, value in combination(rgb): |
paul@32 | 174 | if not c.has_key(value): |
paul@32 | 175 | c[value] = f |
paul@32 | 176 | else: |
paul@32 | 177 | c[value] += f |
paul@26 | 178 | |
paul@20 | 179 | c = [(n, value) for value, n in c.items()] |
paul@20 | 180 | c.sort(reverse=True) |
paul@20 | 181 | colours.append(c) |
paul@10 | 182 | |
paul@20 | 183 | for y, c in enumerate(colours): |
paul@20 | 184 | most = [value for n, value in c[:4]] |
paul@20 | 185 | least = [value for n, value in c[4:]] |
paul@1 | 186 | |
paul@34 | 187 | #if least: |
paul@34 | 188 | # if (0, 0, 0) in least[:2]: |
paul@34 | 189 | # replace((0, 0, 0), most) |
paul@34 | 190 | # if (255, 255, 255) in least[:2]: |
paul@34 | 191 | # replace((255, 255, 255), most) |
paul@33 | 192 | |
paul@20 | 193 | for x in range(0, width): |
paul@20 | 194 | rgb = im.getpixel((x, y)) |
paul@24 | 195 | |
paul@24 | 196 | # Get the requested colours and choose the closest alternative for |
paul@24 | 197 | # less common colours. |
paul@24 | 198 | |
paul@28 | 199 | value = get_value(rgb) |
paul@24 | 200 | if value in least: |
paul@34 | 201 | value = nearest(value, most) |
paul@32 | 202 | |
paul@24 | 203 | im.putpixel((x, y), value) |
paul@1 | 204 | |
paul@0 | 205 | im.save(output_filename) |
paul@0 | 206 | |
paul@0 | 207 | # vim: tabstop=4 expandtab shiftwidth=4 |