1 #!/usr/bin/env python 2 3 from random import random, randrange 4 from os.path import splitext 5 import EXIF 6 import PIL.Image 7 import itertools 8 import math 9 import sys 10 11 corners = [ 12 (0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0), 13 (0, 0, 255), (255, 0, 255), (0, 255, 255), (255, 255, 255) 14 ] 15 16 def within(v, lower, upper): 17 return min(max(v, lower), upper) 18 19 def clip(v): 20 return within(v, 0, 255) 21 22 def distance(rgb1, rgb2): 23 r1, g1, b1 = rgb1 24 r2, g2, b2 = rgb2 25 return math.sqrt(pow(r1 - r2, 2) + pow(g1 - g2, 2) + pow(b1 - b2, 2)) 26 27 def nearest(rgb, values): 28 l = [(distance(rgb, value), value) for value in values] 29 l.sort() 30 return l[0][1] 31 32 def restore(srgb): 33 return tuple(map(lambda x: int(x * 255.0), srgb)) 34 35 def scale(rgb): 36 return tuple(map(lambda x: x / 255.0, rgb)) 37 38 def square(srgb): 39 return tuple(map(lambda x: pow(x, 2), srgb)) 40 41 def invert(srgb): 42 return tuple(map(lambda x: 1.0 - x, srgb)) 43 44 def combination(rgb): 45 rgb = square(scale(rgb)) 46 rgbi = invert(rgb) 47 pairs = zip(rgbi, rgb) 48 d = [] 49 for corner in corners: 50 rs, gs, bs = scale(corner) 51 d.append((pairs[0][int(rs)] * pairs[1][int(gs)] * pairs[2][int(bs)], corner)) 52 return balance(d) 53 54 def complements(rgb): 55 r, g, b = rgb 56 return rgb, restore(invert(scale(rgb))) 57 58 def balance(d): 59 d = dict([(value, f) for f, value in d]) 60 for primary, secondary in map(complements, [(255, 0, 0), (0, 255, 0), (0, 0, 255)]): 61 common = min(d[primary], d[secondary]) 62 d[primary] -= common 63 d[secondary] -= common 64 d[(0, 0, 0)] += common 65 d[(255, 255, 255)] += common 66 return [(f, value) for value, f in d.items()] 67 68 def compensate(d, chosen): 69 dd = dict([(value, f) for f, value in d]) 70 for f, value in d: 71 if value not in chosen: 72 _value, complement = complements(value) 73 if complement in chosen: 74 f = max(0, f - dd[complement]) 75 dd[value] = f 76 return [(f, value) for value, f in dd.items() if value in chosen] 77 78 def combine(d): 79 out = [0, 0, 0] 80 for v, rgb in d: 81 out[0] += v * rgb[0] 82 out[1] += v * rgb[1] 83 out[2] += v * rgb[2] 84 return out 85 86 def pattern(rgb, chosen=None): 87 l = combination(rgb) 88 if chosen: 89 l = compensate(l, chosen) 90 l.sort(reverse=True) 91 return l 92 93 def get_value(rgb, chosen=None): 94 l = pattern(rgb, chosen) 95 limit = sum([f for f, c in l]) 96 choose = random() * limit 97 threshold = 0 98 for f, c in l: 99 threshold += f 100 if choose < threshold: 101 return c 102 return c 103 104 def sign(x): 105 return x >= 0 and 1 or -1 106 107 def saturate_rgb(rgb, exp): 108 return tuple([saturate_value(x, exp) for x in rgb]) 109 110 def saturate_value(x, exp): 111 return int(127.5 + sign(x - 127.5) * 127.5 * pow(abs(x - 127.5) / 127.5, exp)) 112 113 def amplify_rgb(rgb, exp): 114 return tuple([amplify_value(x, exp) for x in rgb]) 115 116 def amplify_value(x, exp): 117 return int(pow(x / 255.0, exp) * 255.0) 118 119 def get_colours(im, width, y): 120 c = {} 121 for x in range(0, width): 122 rgb = im.getpixel((x, y)) 123 124 # Sum the colour probabilities. 125 126 for f, value in combination(rgb): 127 if not c.has_key(value): 128 c[value] = f 129 else: 130 c[value] += f 131 132 c = [(n/width, value) for value, n in c.items()] 133 c.sort(reverse=True) 134 return c 135 136 def test(): 137 size = 512 138 for r in (0, 63, 127, 191, 255): 139 im = PIL.Image.new("RGB", (size, size)) 140 for g in range(0, size): 141 for b in range(0, size): 142 value = get_value((r, (g * 256) / size, (b * 256 / size))) 143 im.putpixel((g, b), value) 144 im.save("rgb%d.png" % r) 145 146 def test_flat(rgb): 147 size = 64 148 im = PIL.Image.new("RGB", (size, size)) 149 for y in range(0, size): 150 for x in range(0, size): 151 im.putpixel((x, y), get_value(rgb)) 152 im.save("rgb%02d%02d%02d.png" % rgb) 153 154 def rotate_and_scale(im, width, height, rotate): 155 if rotate or x and x["Image Orientation"].values == [6L]: 156 im = im.rotate(270) 157 158 w, h = im.size 159 if w > h: 160 height = (width * h) / w 161 else: 162 width = (height * w) / h 163 164 return im.resize((width, height)) 165 166 if __name__ == "__main__": 167 if "--test" in sys.argv: 168 test() 169 sys.exit(0) 170 elif "--test-flat" in sys.argv: 171 test_flat((120, 40, 60)) 172 sys.exit(0) 173 174 width = 320 175 height = 256 176 177 input_filename, output_filename = sys.argv[1:3] 178 basename, ext = splitext(output_filename) 179 preview_filename = "".join([basename + "_preview", ext]) 180 181 rotate = "-r" in sys.argv[3:] 182 saturate = sys.argv[3:].count("-s") 183 desaturate = sys.argv[3:].count("-d") 184 darken = sys.argv[3:].count("-D") 185 brighten = sys.argv[3:].count("-B") 186 preview = "-p" in sys.argv[3:] 187 square = "-2" in sys.argv[3:] and square or (lambda x: x) 188 189 x = EXIF.process_file(open(input_filename)) 190 im = PIL.Image.open(input_filename).convert("RGB") 191 im = rotate_and_scale(im, width, height, rotate) 192 193 width, height = im.size 194 195 if saturate or desaturate or darken or brighten: 196 for y in range(0, height): 197 for x in range(0, width): 198 if saturate or desaturate: 199 rgb = im.getpixel((x, y)) 200 rgb = saturate_rgb(rgb, saturate and math.pow(0.5, saturate) or math.pow(2, desaturate)) 201 im.putpixel((x, y), rgb) 202 203 if darken or brighten: 204 rgb = im.getpixel((x, y)) 205 rgb = amplify_rgb(rgb, brighten and math.pow(0.5, brighten) or math.pow(2, darken)) 206 im.putpixel((x, y), rgb) 207 208 if preview: 209 imp = im.copy() 210 for y in range(0, height): 211 for x in range(0, width): 212 rgb = imp.getpixel((x, y)) 213 value = get_value(rgb) 214 imp.putpixel((x, y), value) 215 imp.save(preview_filename) 216 217 for y in range(0, height): 218 c = get_colours(im, width, y) 219 most = [value for n, value in c[:4]] 220 least = [value for n, value in c[4:]] 221 222 for x in range(0, width): 223 rgb = im.getpixel((x, y)) 224 value = get_value(rgb, most) 225 im.putpixel((x, y), value) 226 227 if y < height - 1: 228 rgbn = im.getpixel((x, y+1)) 229 rgbn = tuple(map(lambda i: clip(i[0] + i[1] - i[2]), zip(rgbn, rgb, value))) 230 im.putpixel((x, y+1), rgbn) 231 232 im.save(output_filename) 233 234 # vim: tabstop=4 expandtab shiftwidth=4