# HG changeset patch # User Paul Boddie # Date 1443717599 -7200 # Node ID cd815af3effc57bd3a92b4861b7bb293ac104af3 # Parent c768f24e5233faecb78a5fa97f1759bed9c2baac Reduced the frequency of the second colour in the dithering pattern. Added preview image generation support. diff -r c768f24e5233 -r cd815af3effc optimiser.py --- a/optimiser.py Thu Oct 01 12:50:55 2015 +0200 +++ b/optimiser.py Thu Oct 01 18:39:59 2015 +0200 @@ -1,7 +1,7 @@ #!/usr/bin/env python -from itertools import combinations from random import randint +from os.path import extsep, splitext import EXIF import PIL.Image import math @@ -50,8 +50,8 @@ return start, end, f def choose(seq, f): - last = int(seq * math.sqrt(f)) - current = int((seq + 1) * math.sqrt(f)) + last = int(seq * f) + current = int((seq + 1) * f) return last != current def get_value(xy, rgb, width, height, values=None): @@ -116,6 +116,10 @@ height = 256 input_filename, output_filename = sys.argv[1:3] + basename, ext = splitext(output_filename) + preview_filename = extsep.join([basename + "_preview", ext]) + + preview = "-p" in sys.argv[3:] rotate = "-r" in sys.argv[3:] saturate = sys.argv[3:].count("-s") desaturate = sys.argv[3:].count("-d") @@ -124,6 +128,9 @@ im = PIL.Image.open(input_filename) im = rotate_and_scale(im, width, height, rotate) + if preview: + im_preview = im.copy() + width, height = im.size colours = [] @@ -147,10 +154,16 @@ else: c[value] += 1 + if preview: + im_preview.putpixel((x, y), value) + c = [(n, value) for value, n in c.items()] c.sort(reverse=True) colours.append(c) + if preview: + im_preview.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:]]