# HG changeset patch # User Paul Boddie # Date 1444488526 -7200 # Node ID be170b4cfcde3c59c95db456707050402e816344 # Parent 7f5faa940ac985dc0703bb75e5651a0de577d9e0 Moved image previewing into the extension module. diff -r 7f5faa940ac9 -r be170b4cfcde optimiser.py --- a/optimiser.py Sat Oct 10 16:41:10 2015 +0200 +++ b/optimiser.py Sat Oct 10 16:48:46 2015 +0200 @@ -72,26 +72,6 @@ return im.resize((width, height)) -def preview_image(pim, half_resolution_preview=False): - - "Return a preview copy of image 'pim'." - - 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 = im.getpixel((x, y)) - value = get_value(rgb) - im.putpixel((x, y), value) - if half_resolution_preview: - im.putpixel((x+1, y), value) - - imp.putdata(im.getdata()) - return imp - def convert_image(pim): "Convert image 'pim' to an appropriate output representation." @@ -219,7 +199,11 @@ # Generate a preview if requested. if preview: - preview_image(im, half_resolution_preview).save(preview_filename) + imp = im.copy() + sim = SimpleImage(list(im.getdata()), im.size) + simp = preview_image(sim, half_resolution_preview) + imp.putdata(simp.getdata()) + imp.save(preview_filename) # Generate an output image if requested. diff -r 7f5faa940ac9 -r be170b4cfcde optimiserlib.py --- a/optimiserlib.py Sat Oct 10 16:41:10 2015 +0200 +++ b/optimiserlib.py Sat Oct 10 16:48:46 2015 +0200 @@ -249,6 +249,27 @@ 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): + + "Return a preview copy of image 'im'." + + width, height = im.size + imp = im.copy() + if half_resolution_preview: + step = 2 + else: + step = 1 + + for y in range(0, height): + for x in range(0, width, step): + rgb = im.getpixel((x, y)) + value = get_value(rgb) + imp.putpixel((x, y), value) + if half_resolution_preview: + imp.putpixel((x+1, y), value) + + return imp + class SimpleImage: "An image behaving like PIL.Image." @@ -294,5 +315,6 @@ count_colours(im, 4) process_image(im, 1.0, 0.0, 1.0, 0.0) + preview_image(im, False) # vim: tabstop=4 expandtab shiftwidth=4