# HG changeset patch # User Paul Boddie # Date 1444173667 -7200 # Node ID 96453447feab2a6fb521b95845d1a6e34c68a77f # Parent 075e5f1f12e316c69e5f2f4c83bcbbf5c3d10fbd Spread any error across two pixels on the following row. diff -r 075e5f1f12e3 -r 96453447feab optimiser.py --- a/optimiser.py Wed Oct 07 00:49:40 2015 +0200 +++ b/optimiser.py Wed Oct 07 01:21:07 2015 +0200 @@ -17,7 +17,7 @@ return min(max(v, lower), upper) def clip(v): - return within(v, 0, 255) + return int(within(v, 0, 255)) def distance(rgb1, rgb2): r1, g1, b1 = rgb1 @@ -249,9 +249,14 @@ im.putpixel((x, y), value) if y < height - 1: - rgbn = im.getpixel((x, y+1)) - rgbn = tuple(map(lambda i: clip(i[0] + i[1] - i[2]), zip(rgbn, rgb, value))) - im.putpixel((x, y+1), rgbn) + + # Spread the error across adjacent pixels. + + error = tuple(map(lambda i: (i[0] - i[1]) / 2.0, zip(rgb, value))) + for xn in range(x, min(x + 2, width)): + rgbn = im.getpixel((xn, y+1)) + rgbn = tuple(map(lambda i: clip(i[0] + i[1]), zip(rgbn, error))) + im.putpixel((xn, y+1), rgbn) im.save(output_filename)