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