# HG changeset patch # User Paul Boddie # Date 1444583535 -7200 # Node ID aaa2938a0ac2b3e467c2aba8642b27d99957d3ff # Parent 904b2295aca58e32e8eef4378028065140ed81dd# Parent 680956899c9e8142ff2881c6283722f707c6353f Replaced "for" loops with "while" loops for extra performance. diff -r 904b2295aca5 -r aaa2938a0ac2 optimiser.py diff -r 904b2295aca5 -r aaa2938a0ac2 optimiserlib.py --- a/optimiserlib.py Sun Oct 11 18:07:59 2015 +0200 +++ b/optimiserlib.py Sun Oct 11 19:12:15 2015 +0200 @@ -181,7 +181,8 @@ width, height = im.size c = {} - for x in range(0, width): + x = 0 + while x < width: rgb = im.getpixel((x, y)) # Sum the colour probabilities. @@ -192,6 +193,8 @@ else: c[value] += f + x += 1 + d = [(n/width, value) for value, n in c.items()] d.sort(reverse=True) return d @@ -222,12 +225,16 @@ width, height = im.size - for y in range(0, height): + y = 0 + while y < height: l = set() - for x in range(0, width): + x = 0 + while x < width: l.add(im.getpixel((x, y))) + x += 1 if len(l) > colours: return (y, l) + y += 1 return None def process_image(im, saturate, desaturate, darken, brighten): @@ -240,14 +247,18 @@ width, height = im.size if saturate or desaturate or darken or brighten: - for y in range(0, height): - for x in range(0, width): + y = 0 + while y < height: + x = 0 + while x < width: rgb = im.getpixel((x, y)) if saturate or desaturate: rgb = saturate_rgb(rgb, saturate and 0.5 / saturate or 2.0 * desaturate) if darken or brighten: rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2.0 * darken) im.putpixel((x, y), rgb) + x += 1 + y += 1 def convert_image(im, colours): @@ -255,7 +266,8 @@ width, height = im.size - for y in range(0, height): + y = 0 + while y < height: c = get_colours(im, y) suggestions = [] @@ -264,11 +276,13 @@ most = [value for f, value in l] missing = 0 - for x in range(0, width): + x = 0 + while x < width: rgb = im.getpixel((x, y)) value = get_value(rgb, most, True) if value is None: missing += 1 + x += 1 if not missing: break # use this combination @@ -280,7 +294,8 @@ suggestions.sort() most = [value for f, value in suggestions[0][1]] # get the combination - for x in range(0, width): + x = 0 + while x < width: rgb = im.getpixel((x, y)) value = get_value(rgb, most) im.putpixel((x, y), value) @@ -303,6 +318,10 @@ ) im.putpixel((x, y+1), rgbn) + x += 1 + + y += 1 + class SimpleImage: "An image behaving like PIL.Image."