# HG changeset patch # User Paul Boddie # Date 1444686674 -7200 # Node ID c97a0dbd98d81a15c40ed61baa8ac35519f41451 # Parent 62cdce59435f584fb310606bf622fff0657c8d4e# Parent 03ec9c57b5a003a134b6154b6303449f98de6ddb Separated scaling and width-stretching operations. diff -r 62cdce59435f -r c97a0dbd98d8 optimiser.py --- a/optimiser.py Mon Oct 12 20:46:03 2015 +0200 +++ b/optimiser.py Mon Oct 12 23:51:14 2015 +0200 @@ -50,7 +50,7 @@ im.putpixel((x, y), get_value(rgb)) im.save("rgb%02d%02d%02d.png" % rgb) -def rotate_and_scale(exif, im, width, height, rotate): +def rotate_and_scale(exif, im, width, height, rotate, scale_factor): """ Using the given 'exif' information, rotate and scale image 'im' given the @@ -64,14 +64,19 @@ w, h = im.size - width_scale_factor = float(width) / w + # Get the relationship between the base width and the image width. + + width_scale_factor = (width / scale_factor) / w height_scale_factor = float(height) / h - scale_factor = min(width_scale_factor, height_scale_factor) + min_scale_factor = min(width_scale_factor, height_scale_factor) - if scale_factor < 1: - width = int(scale_factor * w) - height = int(scale_factor * h) + if min_scale_factor < 1: + width = int(min_scale_factor * w * scale_factor) + height = int(min_scale_factor * h) return im.resize((width, height)) + elif scale_factor != 1: + width = int(w * scale_factor) + return im.resize((width, h)) else: return im @@ -158,10 +163,9 @@ number_of_colours = get_parameter(options, "-C", int, 4, 4) preserve_aspect_ratio = "-A" in options - # Determine whether the height will need adjusting before conversion. + # Determine any differing horizontal scale factor. scale_factor = float(width) / base_width - height = int(base_height * scale_factor) # Preprocessing options that employ parameters. @@ -188,15 +192,9 @@ 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, width, height, rotate, scale_factor) image_width, image_height = im.size - # Scale images to the appropriate height. - - if scale_factor != 1: - im = im.resize((image_width, int(image_height / scale_factor))) - image_width, image_height = im.size - sim = SimpleImage(list(im.getdata()), im.size) process_image(sim, saturate, desaturate, darken, brighten) im.putdata(sim.getdata())