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@32 | 21 | def distribution(rgb, values=None): |
paul@32 | 22 | l = [] |
paul@32 | 23 | total = 0 |
paul@32 | 24 | for c in values or corners: |
paul@32 | 25 | d = distance(rgb, c) |
paul@32 | 26 | if d == 0: |
paul@32 | 27 | return [(1, c)] |
paul@32 | 28 | l.append((pow(d, -3), c)) |
paul@32 | 29 | total += pow(d, -3) |
paul@32 | 30 | return [(d / total, c) for d, c in l] |
paul@20 | 31 | |
paul@32 | 32 | def pattern(rgb, values=None): |
paul@32 | 33 | l = distribution(rgb, values or corners) |
paul@32 | 34 | l.sort(reverse=True) |
paul@20 | 35 | return l |
paul@19 | 36 | |
paul@28 | 37 | def get_value(rgb, values=None): |
paul@32 | 38 | choose = random() |
paul@32 | 39 | threshold = 0 |
paul@32 | 40 | for f, c in pattern(rgb, values): |
paul@32 | 41 | threshold += f |
paul@32 | 42 | if choose < threshold: |
paul@32 | 43 | return c |
paul@32 | 44 | return c |
paul@0 | 45 | |
paul@21 | 46 | def sign(x): |
paul@21 | 47 | return x >= 0 and 1 or -1 |
paul@21 | 48 | |
paul@22 | 49 | def saturate_rgb(rgb, exp): |
paul@22 | 50 | return tuple([saturate_value(x, exp) for x in rgb]) |
paul@21 | 51 | |
paul@22 | 52 | def saturate_value(x, exp): |
paul@24 | 53 | return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp)) |
paul@21 | 54 | |
paul@20 | 55 | def test(): |
paul@20 | 56 | size = 512 |
paul@20 | 57 | for r in (0, 63, 127, 191, 255): |
paul@20 | 58 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 59 | for g in range(0, size): |
paul@20 | 60 | for b in range(0, size): |
paul@28 | 61 | value = get_value((r, (g * 256) / size, (b * 256 / size))) |
paul@20 | 62 | im.putpixel((g, b), value) |
paul@20 | 63 | im.save("rgb%d.png" % r) |
paul@20 | 64 | |
paul@20 | 65 | def test_flat(rgb): |
paul@20 | 66 | size = 64 |
paul@20 | 67 | im = PIL.Image.new("RGB", (size, size)) |
paul@20 | 68 | for y in range(0, size): |
paul@20 | 69 | for x in range(0, size): |
paul@28 | 70 | im.putpixel((x, y), get_value(rgb)) |
paul@20 | 71 | im.save("rgb%02d%02d%02d.png" % rgb) |
paul@20 | 72 | |
paul@20 | 73 | def rotate_and_scale(im, width, height, rotate): |
paul@19 | 74 | if rotate or x and x["Image Orientation"].values == [6L]: |
paul@11 | 75 | im = im.rotate(270) |
paul@19 | 76 | |
paul@4 | 77 | w, h = im.size |
paul@19 | 78 | if w > h: |
paul@19 | 79 | height = (width * h) / w |
paul@19 | 80 | else: |
paul@19 | 81 | width = (height * w) / h |
paul@19 | 82 | |
paul@20 | 83 | return im.resize((width, height)) |
paul@1 | 84 | |
paul@20 | 85 | if __name__ == "__main__": |
paul@20 | 86 | if "--test" in sys.argv: |
paul@20 | 87 | test() |
paul@20 | 88 | sys.exit(0) |
paul@20 | 89 | elif "--test-flat" in sys.argv: |
paul@20 | 90 | test_flat((120, 40, 60)) |
paul@20 | 91 | sys.exit(0) |
paul@0 | 92 | |
paul@20 | 93 | width = 320 |
paul@20 | 94 | height = 256 |
paul@20 | 95 | |
paul@20 | 96 | input_filename, output_filename = sys.argv[1:3] |
paul@26 | 97 | basename, ext = splitext(output_filename) |
paul@26 | 98 | |
paul@20 | 99 | rotate = "-r" in sys.argv[3:] |
paul@23 | 100 | saturate = sys.argv[3:].count("-s") |
paul@23 | 101 | desaturate = sys.argv[3:].count("-d") |
paul@1 | 102 | |
paul@20 | 103 | x = EXIF.process_file(open(input_filename)) |
paul@32 | 104 | im = PIL.Image.open(input_filename).convert("RGB") |
paul@20 | 105 | im = rotate_and_scale(im, width, height, rotate) |
paul@1 | 106 | |
paul@20 | 107 | width, height = im.size |
paul@20 | 108 | |
paul@20 | 109 | colours = [] |
paul@10 | 110 | |
paul@20 | 111 | for y in range(0, height): |
paul@20 | 112 | c = {} |
paul@20 | 113 | for x in range(0, width): |
paul@20 | 114 | rgb = im.getpixel((x, y)) |
paul@24 | 115 | |
paul@24 | 116 | # Saturate if requested. |
paul@24 | 117 | |
paul@22 | 118 | if saturate or desaturate: |
paul@23 | 119 | rgb = saturate_rgb(rgb, saturate and math.pow(0.5, saturate) or math.pow(2, desaturate)) |
paul@24 | 120 | im.putpixel((x, y), rgb) |
paul@24 | 121 | |
paul@32 | 122 | # Sum the colour probabilities. |
paul@24 | 123 | |
paul@32 | 124 | for f, value in distribution(rgb): |
paul@32 | 125 | if not c.has_key(value): |
paul@32 | 126 | c[value] = f |
paul@32 | 127 | else: |
paul@32 | 128 | c[value] += f |
paul@26 | 129 | |
paul@20 | 130 | c = [(n, value) for value, n in c.items()] |
paul@20 | 131 | c.sort(reverse=True) |
paul@20 | 132 | colours.append(c) |
paul@10 | 133 | |
paul@20 | 134 | for y, c in enumerate(colours): |
paul@20 | 135 | most = [value for n, value in c[:4]] |
paul@20 | 136 | least = [value for n, value in c[4:]] |
paul@1 | 137 | |
paul@20 | 138 | for x in range(0, width): |
paul@20 | 139 | rgb = im.getpixel((x, y)) |
paul@24 | 140 | |
paul@24 | 141 | # Get the requested colours and choose the closest alternative for |
paul@24 | 142 | # less common colours. |
paul@24 | 143 | |
paul@28 | 144 | value = get_value(rgb) |
paul@24 | 145 | if value in least: |
paul@32 | 146 | rgb = im.getpixel((x, y)) |
paul@32 | 147 | value = pattern(value, most)[0][1] |
paul@32 | 148 | |
paul@24 | 149 | im.putpixel((x, y), value) |
paul@1 | 150 | |
paul@0 | 151 | im.save(output_filename) |
paul@0 | 152 | |
paul@0 | 153 | # vim: tabstop=4 expandtab shiftwidth=4 |