# HG changeset patch # User Paul Boddie # Date 1444486389 -7200 # Node ID 3081e03b8d951a29b2b2da25307f8b4c28ef8eac # Parent ab5c1ce7de8f3717eb631d4464fe0fceb4fc381c Moved more functions into the extension module. diff -r ab5c1ce7de8f -r 3081e03b8d95 optimiser.py --- a/optimiser.py Sat Oct 10 16:04:09 2015 +0200 +++ b/optimiser.py Sat Oct 10 16:13:09 2015 +0200 @@ -28,27 +28,6 @@ # Image operations. -def get_colours(im, y): - - "Get a colour distribution from image 'im' for the row 'y'." - - width, height = im.size - c = {} - for x in range(0, width): - rgb = im.getpixel((x, y)) - - # Sum the colour probabilities. - - for f, value in combination(rgb): - if not c.has_key(value): - c[value] = f - else: - c[value] += f - - d = [(n/width, value) for value, n in c.items()] - d.sort(reverse=True) - return d - def test(): "Generate slices of the colour cube." @@ -93,24 +72,6 @@ return im.resize((width, height)) -def count_colours(im, colours): - - """ - Count colours on each row of image 'im', returning a tuple indicating the - first row with more than the given number of 'colours' together with the - found colours; otherwise returning None. - """ - - width, height = im.size - - for y in range(0, height): - l = set() - for x in range(0, width): - l.add(im.getpixel((x, y))) - if len(l) > colours: - return (y, l) - return None - def process_image(pim, saturate, desaturate, darken, brighten): """ diff -r ab5c1ce7de8f -r 3081e03b8d95 optimiserlib.py --- a/optimiserlib.py Sat Oct 10 16:04:09 2015 +0200 +++ b/optimiserlib.py Sat Oct 10 16:13:09 2015 +0200 @@ -175,6 +175,27 @@ # Image operations. +def get_colours(im, y): + + "Get a colour distribution from image 'im' for the row 'y'." + + width, height = im.size + c = {} + for x in range(0, width): + rgb = im.getpixel((x, y)) + + # Sum the colour probabilities. + + for f, value in combination(rgb): + if not c.has_key(value): + c[value] = f + else: + c[value] += f + + d = [(n/width, value) for value, n in c.items()] + d.sort(reverse=True) + return d + def get_combinations(c, n): """ @@ -191,6 +212,24 @@ all.sort(reverse=True) return [l for total, l in all] +def count_colours(im, colours): + + """ + Count colours on each row of image 'im', returning a tuple indicating the + first row with more than the given number of 'colours' together with the + found colours; otherwise returning None. + """ + + width, height = im.size + + for y in range(0, height): + l = set() + for x in range(0, width): + l.add(im.getpixel((x, y))) + if len(l) > colours: + return (y, l) + return None + class SimpleImage: "An image behaving like PIL.Image." @@ -232,4 +271,7 @@ im2.putpixel((0, 0), (255, 255, 255)) im2.getdata() == [(255, 255, 255), (0, 0, 0)] + get_colours(im, 0) == [(1.0, (0, 0, 0))] + count_colours(im, 4) + # vim: tabstop=4 expandtab shiftwidth=4