# HG changeset patch # User Paul Boddie # Date 1444421809 -7200 # Node ID 5b3e85002c1048fd41536b731b48c3a9b0bc63b4 # Parent 92dade31295323c9972c62a1de810d7123730c74 Moved various activities to their own functions. diff -r 92dade312953 -r 5b3e85002c10 optimiser.py --- a/optimiser.py Fri Oct 09 21:58:23 2015 +0200 +++ b/optimiser.py Fri Oct 09 22:16:49 2015 +0200 @@ -261,6 +261,7 @@ """ width, height = im.size + for y in range(0, height): l = set() for x in range(0, width): @@ -269,6 +270,80 @@ return (y, l) return None +def process_image(im, saturate, desaturate, darken, brighten): + + """ + Process image 'im' using the given options: 'saturate', 'desaturate', + 'darken', 'brighten'. + """ + + width, height = im.size + + if saturate or desaturate or darken or brighten: + for y in range(0, height): + for x in range(0, 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) + +def preview_image(im, half_resolution_preview=False): + + "Return a preview copy of image 'im'." + + width, height = im.size + + imp = im.copy() + 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)) + value = get_value(rgb) + imp.putpixel((x, y), value) + if half_resolution_preview: + imp.putpixel((x+1, y), value) + + return imp + +def convert_image(im): + + "Convert image 'im' to an appropriate output representation." + + width, height = im.size + + for y in range(0, height): + c = get_colours(im, y) + + for l in get_combinations(c, 4): + most = [value for f, value in l] + for x in range(0, width): + rgb = im.getpixel((x, y)) + value = get_value(rgb, most, True) + if value is None: + break # try next combination + else: + break # use this combination + else: + most = [value for f, value in c[:4]] # use the first four + + for x in range(0, width): + rgb = im.getpixel((x, y)) + value = get_value(rgb, most) + im.putpixel((x, y), value) + + if x < width - 1: + rgbn = im.getpixel((x+1, y)) + rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 4.0), zip(rgbn, rgb, value))) + im.putpixel((x+1, y), rgbn) + + if y < height - 1: + rgbn = im.getpixel((x, y+1)) + rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 2.0), zip(rgbn, rgb, value))) + im.putpixel((x, y+1), rgbn) + def get_float(options, flag): try: i = options.index(flag) @@ -342,66 +417,17 @@ im = PIL.Image.open(input_filename).convert("RGB") im = rotate_and_scale(exif, im, width, height, rotate) - width, height = im.size - - if saturate or desaturate or darken or brighten: - for y in range(0, height): - for x in range(0, 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) + process_image(im, saturate, desaturate, darken, brighten) # Generate a preview if requested. if preview: - imp = im.copy() - 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)) - value = get_value(rgb) - imp.putpixel((x, y), value) - if half_resolution_preview: - imp.putpixel((x+1, y), value) - - imp.save(preview_filename) + preview_image(im, half_resolution_preview).save(preview_filename) # Generate an output image if requested. if make_image: - for y in range(0, height): - c = get_colours(im, y) - - for l in get_combinations(c, 4): - most = [value for f, value in l] - for x in range(0, width): - rgb = im.getpixel((x, y)) - value = get_value(rgb, most, True) - if value is None: - break # try next combination - else: - break # use this combination - else: - most = [value for f, value in c[:4]] # use the first four - - for x in range(0, width): - rgb = im.getpixel((x, y)) - value = get_value(rgb, most) - im.putpixel((x, y), value) - - if x < width - 1: - rgbn = im.getpixel((x+1, y)) - rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 4.0), zip(rgbn, rgb, value))) - im.putpixel((x+1, y), rgbn) - - if y < height - 1: - rgbn = im.getpixel((x, y+1)) - rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 2.0), zip(rgbn, rgb, value))) - im.putpixel((x, y+1), rgbn) - + convert_image(im) im.save(output_filename) # Verify the output image (which may be loaded) if requested.