# HG changeset patch # User Paul Boddie # Date 1444582255 -7200 # Node ID 680956899c9e8142ff2881c6283722f707c6353f # Parent 854421107591382a6160f85f09a1c09792024e56# Parent 58016858bada9d7695bc603060b01871e560b32a Replaced "for" loops with "while" loops for extra performance. diff -r 854421107591 -r 680956899c9e optimiser.py --- a/optimiser.py Sun Oct 11 15:56:31 2015 +0200 +++ b/optimiser.py Sun Oct 11 18:50:55 2015 +0200 @@ -182,7 +182,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. @@ -193,6 +194,8 @@ else: c[value] += f + x += 1 + d = [(n/width, value) for value, n in c.items()] d.sort(reverse=True) return d @@ -232,9 +235,13 @@ size = 64 im = PIL.Image.new("RGB", (size, size)) - for y in range(0, size): - for x in range(0, size): + y = 0 + while y < height: + x = 0 + while x < width: im.putpixel((x, y), get_value(rgb)) + x += 1 + y += 1 im.save("rgb%02d%02d%02d.png" % rgb) def rotate_and_scale(exif, im, width, height, rotate): @@ -267,12 +274,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(pim, saturate, desaturate, darken, brighten): @@ -286,14 +297,18 @@ im = SimpleImage(list(pim.getdata()), pim.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 * desaturate) if darken or brighten: rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2 * darken) im.putpixel((x, y), rgb) + x += 1 + y += 1 pim.putdata(im.getdata()) @@ -304,7 +319,8 @@ width, height = pim.size im = SimpleImage(list(pim.getdata()), pim.size) - for y in range(0, height): + y = 0 + while y < height: c = get_colours(im, y) suggestions = [] @@ -313,11 +329,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 @@ -329,7 +347,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) @@ -352,6 +371,10 @@ ) im.putpixel((x, y+1), rgbn) + x += 1 + + y += 1 + pim.putdata(im.getdata()) def get_parameter(options, flag, conversion, default, missing):