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