# HG changeset patch # User Paul Boddie # Date 1443892091 -7200 # Node ID baa08ae98f2e88bd6a5d30e5980b768a0b0b5bb3 # Parent 9060af8f5ef50aa1c76dc398594f0b3353a1e1d1 Added a "balancing" function to remove primary/secondary complement pairs, thus removing colour noise and simplifying the job of the optimisation routine. Added the useful preview function back to the program. Removed some working notes. diff -r 9060af8f5ef5 -r baa08ae98f2e optimiser.py --- a/optimiser.py Sat Oct 03 16:52:17 2015 +0200 +++ b/optimiser.py Sat Oct 03 19:08:11 2015 +0200 @@ -23,6 +23,9 @@ l.sort() return l[0][1] +def restore(srgb): + return tuple(map(lambda x: int(x * 255.0), srgb)) + def scale(rgb): return tuple(map(lambda x: x / 255.0, rgb)) @@ -37,7 +40,21 @@ for corner in corners: rs, gs, bs = scale(corner) d.append((pairs[0][int(rs)] * pairs[1][int(gs)] * pairs[2][int(bs)], corner)) - return d + return balance(d) + +def complements(rgb): + r, g, b = rgb + return rgb, restore(invert(scale(rgb))) + +def balance(d): + d = dict([(value, f) for f, value in d]) + for primary, secondary in map(complements, [(255, 0, 0), (0, 255, 0), (0, 0, 255)]): + common = min(d[primary], d[secondary]) + d[primary] -= common + d[secondary] -= common + d[(0, 0, 0)] += common + d[(255, 255, 255)] += common + return [(f, value) for value, f in d.items()] def combine(d): out = [0, 0, 0] @@ -47,30 +64,6 @@ out[2] += v * rgb[2] return out -""" -r(R - K) -r(Y - G) -r(M - B) -r(W - C) -g(G - K) -g(Y - R) -g(C - B) -g(W - M) -b(B - K) -b(M - R) -b(C - G) -b(W - Y) - -b(g(r(W - C) - r(M - B)) - g(r(Y - G) - r(R - K))) -b(r(g(W - M) - g(C - B)) - r(g(Y - R) - g(G - K))) - -b(g(rW + riC) + gi(rM + riB)) + bi(g(rY + riG) + gi(rR + riK)) -b(r(gW + giM) + ri(gC + giB)) + bi(r(gY + giR) + ri(gG + giK)) - -W(b.g.r) + C(b.g.ri) + M(b.gi.r) + B(b.g.ri) + Y(bi.g.r) + G(bi.g.ri) + R(bi.gi.r) + K(bi.gi.ri) -... -""" - def pattern(rgb): l = combination(rgb) l.sort(reverse=True) @@ -144,10 +137,12 @@ input_filename, output_filename = sys.argv[1:3] basename, ext = splitext(output_filename) + preview_filename = "".join([basename + "_preview", ext]) rotate = "-r" in sys.argv[3:] saturate = sys.argv[3:].count("-s") desaturate = sys.argv[3:].count("-d") + preview = "-p" in sys.argv[3:] x = EXIF.process_file(open(input_filename)) im = PIL.Image.open(input_filename).convert("RGB") @@ -180,6 +175,15 @@ c.sort(reverse=True) colours.append(c) + if preview: + imp = im.copy() + for y in range(0, height): + for x in range(0, width): + rgb = imp.getpixel((x, y)) + value = get_value(rgb) + imp.putpixel((x, y), value) + imp.save(preview_filename) + for y, c in enumerate(colours): most = [value for n, value in c[:4]] least = [value for n, value in c[4:]]