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