# HG changeset patch # User Paul Boddie # Date 1444480885 -7200 # Node ID 429cc2ef9b9f6025b616be866f5b145a1c07e556 # Parent 5b3e85002c1048fd41536b731b48c3a9b0bc63b4 Experiment with a simple alternative class to Image for pixel access. diff -r 5b3e85002c10 -r 429cc2ef9b9f optimiser.py --- a/optimiser.py Fri Oct 09 22:16:49 2015 +0200 +++ b/optimiser.py Sat Oct 10 14:41:25 2015 +0200 @@ -188,9 +188,9 @@ else: c[value] += f - c = [(n/width, value) for value, n in c.items()] - c.sort(reverse=True) - return c + d = [(n/width, value) for value, n in c.items()] + d.sort(reverse=True) + return d def get_combinations(c, n): @@ -270,14 +270,15 @@ return (y, l) return None -def process_image(im, saturate, desaturate, darken, brighten): +def process_image(pim, saturate, desaturate, darken, brighten): """ - Process image 'im' using the given options: 'saturate', 'desaturate', + Process image 'pim' using the given options: 'saturate', 'desaturate', 'darken', 'brighten'. """ - width, height = im.size + width, height = pim.size + im = SimpleImage(list(pim.getdata()), pim.size) if saturate or desaturate or darken or brighten: for y in range(0, height): @@ -289,30 +290,34 @@ rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2 * darken) im.putpixel((x, y), rgb) -def preview_image(im, half_resolution_preview=False): + pim.putdata(im.getdata()) - "Return a preview copy of image 'im'." +def preview_image(pim, half_resolution_preview=False): - width, height = im.size + "Return a preview copy of image 'pim'." - imp = im.copy() + width, height = pim.size + imp = pim.copy() + im = SimpleImage(list(pim.getdata()), pim.size) step = half_resolution_preview and 2 or 1 for y in range(0, height): - for x in range(0, width, step): - rgb = imp.getpixel((x, y)) + for x in range(0, width): + rgb = im.getpixel((x, y)) value = get_value(rgb) - imp.putpixel((x, y), value) + im.putpixel((x, y), value) if half_resolution_preview: - imp.putpixel((x+1, y), value) + im.putpixel((x+1, y), value) + imp.putdata(im.getdata()) return imp -def convert_image(im): +def convert_image(pim): - "Convert image 'im' to an appropriate output representation." + "Convert image 'pim' to an appropriate output representation." - width, height = im.size + width, height = pim.size + im = SimpleImage(list(pim.getdata()), pim.size) for y in range(0, height): c = get_colours(im, y) @@ -344,6 +349,8 @@ rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 2.0), zip(rgbn, rgb, value))) im.putpixel((x, y+1), rgbn) + pim.putdata(im.getdata()) + def get_float(options, flag): try: i = options.index(flag) @@ -354,6 +361,28 @@ except ValueError: return 0.0 +class SimpleImage: + + "An image behaving like PIL.Image." + + def __init__(self, data, size): + self.data = data + self.width, self.height = self.size = size + + def copy(self): + return SimpleImage(self.data[:], self.size) + + def getpixel(self, xy): + x, y = xy + return self.data[y * self.width + x] + + def putpixel(self, xy, value): + x, y = xy + self.data[y * self.width + x] = value + + def getdata(self): + return self.data + # Main program. if __name__ == "__main__":