# HG changeset patch # User Paul Boddie # Date 1444570452 -7200 # Node ID a879c7d1d28e7f47144169d350ace2ea3e341880 # Parent 2cc185ffe5fddce9a8c98f723b18e83a92bd25f2# Parent e5fce4442d7ea21c9b8fb376253794e21d71cba5 Combined the preview and conversion functions, added configuration of the number of chosen colours and the output image width. Fixed option descriptions. diff -r 2cc185ffe5fd -r a879c7d1d28e optimiser.py --- a/optimiser.py Sun Oct 11 00:31:34 2015 +0200 +++ b/optimiser.py Sun Oct 11 15:34:12 2015 +0200 @@ -72,15 +72,23 @@ return im.resize((width, height)) -def get_float(options, flag): +def get_parameter(options, flag, conversion, default, missing): + + """ + From 'options', return any parameter following the given 'flag', applying + the 'conversion' which has the given 'default' if no valid parameter is + found, or returning the given 'missing' value if the flag does not appear at + all. + """ + try: i = options.index(flag) try: - return float(options[i+1]) + return conversion(options[i+1]) except (IndexError, ValueError): - return 1.0 + return default except ValueError: - return 0.0 + return missing # Main program. @@ -100,10 +108,13 @@ Options are... --s - Saturate the input image (can be followed by a float, default 1.0) --d - Desaturate the input image (can be followed by a float, default 1.0) --D - Darken the input image (can be followed by a float, default 1.0) --B - Brighten the input image (can be followed by a float, default 1.0) +-W - Indicate the output width (default is 320) +-C - Number of colours per scanline (default is 4) + +-s - Saturate the input image (optional float, 1.0 if unspecified) +-d - Desaturate the input image (optional float, 1.0 if unspecified) +-D - Darken the input image (optional float, 1.0 if unspecified) +-B - Brighten the input image (optional float, 1.0 if unspecified) -r - Rotate the input image clockwise -p - Generate a separate preview image @@ -113,7 +124,7 @@ """ % split(sys.argv[0])[1] sys.exit(1) - width = 320 + base_width = 320 height = 256 input_filename, output_filename = sys.argv[1:3] @@ -122,12 +133,17 @@ options = sys.argv[3:] - # Preprocessing options that can be repeated for extra effect. + # Basic image properties. + + width = get_parameter(options, "-W", int, base_width, base_width) + number_of_colours = get_parameter(options, "-C", int, 4, 4) - saturate = get_float(options, "-s") - desaturate = get_float(options, "-d") - darken = get_float(options, "-D") - brighten = get_float(options, "-B") + # Preprocessing options that employ parameters. + + saturate = get_parameter(options, "-s", float, 1.0, 0.0) + desaturate = get_parameter(options, "-d", float, 1.0, 0.0) + darken = get_parameter(options, "-D", float, 1.0, 0.0) + brighten = get_parameter(options, "-B", float, 1.0, 0.0) # General output options. @@ -143,7 +159,12 @@ if make_image or preview: exif = EXIF.process_file(open(input_filename)) im = PIL.Image.open(input_filename).convert("RGB") - im = rotate_and_scale(exif, im, width, height, rotate) + im = rotate_and_scale(exif, im, base_width, height, rotate) + + # Scale images to the appropriate width. + + if width != base_width: + im = im.resize((width, height)) sim = SimpleImage(list(im.getdata()), im.size) process_image(sim, saturate, desaturate, darken, brighten) @@ -153,16 +174,20 @@ if preview: imp = im.copy() - sim = SimpleImage(list(im.getdata()), im.size) - simp = preview_image(sim, half_resolution_preview) - imp.putdata(simp.getdata()) + if half_resolution_preview: + imp = imp.resize((width / 2, height)) + sim = SimpleImage(list(imp.getdata()), imp.size) + convert_image(sim, 8) + imp.putdata(sim.getdata()) + if half_resolution_preview: + imp = imp.resize((width, height)) imp.save(preview_filename) # Generate an output image if requested. if make_image: sim = SimpleImage(list(im.getdata()), im.size) - convert_image(sim) + convert_image(sim, number_of_colours) im.putdata(sim.getdata()) im.save(output_filename) @@ -173,7 +198,7 @@ im = PIL.Image.open(output_filename).convert("RGB") im = SimpleImage(list(im.getdata()), im.size) - result = count_colours(im, 4) + result = count_colours(im, number_of_colours) if result is not None: y, colours = result print "Image %s: row %d has the following colours: %s" % (output_filename, y, "; ".join([repr(c) for c in colours])) diff -r 2cc185ffe5fd -r a879c7d1d28e optimiserlib.py --- a/optimiserlib.py Sun Oct 11 00:31:34 2015 +0200 +++ b/optimiserlib.py Sun Oct 11 15:34:12 2015 +0200 @@ -249,28 +249,7 @@ rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2.0 * 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 - -def convert_image(im): +def convert_image(im, colours): "Convert image 'im' to an appropriate output representation." @@ -279,7 +258,7 @@ for y in range(0, height): c = get_colours(im, y) - for l in get_combinations(c, 4): + for l in get_combinations(c, colours): most = [value for f, value in l] for x in range(0, width): rgb = im.getpixel((x, y)) @@ -289,7 +268,7 @@ else: break # use this combination else: - most = [value for f, value in c[:4]] # use the first four + most = [value for f, value in c[:colours]] # use the first colours for x in range(0, width): rgb = im.getpixel((x, y)) @@ -359,7 +338,6 @@ count_colours(im, 4) process_image(im, 1.0, 0.0, 1.0, 0.0) - preview_image(im, False) - convert_image(im) + convert_image(im, 4) # vim: tabstop=4 expandtab shiftwidth=4