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