# HG changeset patch # User Paul Boddie # Date 1444603730 -7200 # Node ID dac076310b26459a41cecd169c0148a3d5aaf569 # Parent 2ddb5ff4b7c921918934a41f9b4ec567117d94af# Parent 649cbff60e7c62b04b83967de6d1045bdd43f72d Fixed scaling calculations; added explicit support to preserve the aspect ratio. diff -r 2ddb5ff4b7c9 -r dac076310b26 optimiser.py --- a/optimiser.py Sun Oct 11 23:31:25 2015 +0200 +++ b/optimiser.py Mon Oct 12 00:48:50 2015 +0200 @@ -108,6 +108,8 @@ -W - Indicate the output width (default is 320) -C - Number of colours per scanline (default is 4) +-A - Produce an output image with the same aspect ratio as the input + (useful for previewing) -s - Saturate the input image (optional float, 1.0 if unspecified) -d - Desaturate the input image (optional float, 1.0 if unspecified) @@ -129,8 +131,8 @@ """ % split(sys.argv[0])[1] sys.exit(1) - base_width = 320 - height = 256 + base_width = width = 320 + base_height = height = 256 input_filename, output_filename = sys.argv[1:3] basename, ext = splitext(output_filename) @@ -142,6 +144,10 @@ width = get_parameter(options, "-W", int, base_width, base_width) number_of_colours = get_parameter(options, "-C", int, 4, 4) + preserve_aspect_ratio = "-A" in options + + scale_factor = float(width) / base_width + height = int(base_height * scale_factor) # Preprocessing options that employ parameters. @@ -165,12 +171,13 @@ 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, base_width, height, rotate) + im = rotate_and_scale(exif, im, width, height, rotate) + image_width, image_height = im.size - # Scale images to the appropriate width. + # Scale images to the appropriate height. - if width != base_width: - im = im.resize((width, height)) + if scale_factor != 1: + im = im.resize((image_width, int(image_height / scale_factor))) sim = SimpleImage(list(im.getdata()), im.size) process_image(sim, saturate, desaturate, darken, brighten) @@ -181,12 +188,18 @@ if preview: imp = im.copy() if half_resolution_preview: - imp = imp.resize((width / 2, height)) + imp = imp.resize((image_width / 2, image_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 = imp.resize((image_width, image_height)) + + # Scale images to a height determined by the aspect ratio. + + if preserve_aspect_ratio and scale_factor != 1: + imp = imp.resize((image_width, image_height)) + imp.save(preview_filename) # Generate an output image if requested. @@ -195,6 +208,12 @@ sim = SimpleImage(list(im.getdata()), im.size) convert_image(sim, number_of_colours, least_error) im.putdata(sim.getdata()) + + # Scale images to a height determined by the aspect ratio. + + if preserve_aspect_ratio and scale_factor != 1: + im = im.resize((image_width, image_height)) + im.save(output_filename) # Verify the output image (which may be loaded) if requested.