# HG changeset patch # User Paul Boddie # Date 1444686588 -7200 # Node ID 363ec82436bd099f818227efc9c19e3533c1f978 # Parent 0d27eee1a2bba27589f4d4cc0901ac7626987328 Separated scaling and width-stretching operations. diff -r 0d27eee1a2bb -r 363ec82436bd optimiser.py --- a/optimiser.py Mon Oct 12 20:45:09 2015 +0200 +++ b/optimiser.py Mon Oct 12 23:49:48 2015 +0200 @@ -54,7 +54,7 @@ y += 1 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 @@ -68,14 +68,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 @@ -162,10 +167,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. @@ -192,15 +196,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 - process_image(im, saturate, desaturate, darken, brighten) # Generate a preview if requested.