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