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