# HG changeset patch # User Paul Boddie # Date 1444675530 -7200 # Node ID 93aaf33edc7325b6e0c446e30078c9a8035b82fa # Parent fcd8939da191eb0931fb3f221e2a22edc3adf193# Parent 0d27eee1a2bba27589f4d4cc0901ac7626987328 Made more thorough fixes to the scaling of images. diff -r fcd8939da191 -r 93aaf33edc73 optimiser.py --- a/optimiser.py Mon Oct 12 18:10:28 2015 +0200 +++ b/optimiser.py Mon Oct 12 20:45:30 2015 +0200 @@ -67,12 +67,17 @@ im = im.rotate(270) w, h = im.size - if w > h: - height = (width * h) / w + + width_scale_factor = float(width) / w + height_scale_factor = float(height) / h + scale_factor = min(width_scale_factor, height_scale_factor) + + if scale_factor < 1: + width = int(scale_factor * w) + height = int(scale_factor * h) + return im.resize((width, height)) else: - width = (height * w) / h - - return im.resize((width, height)) + return im def get_parameter(options, flag, conversion, default, missing): @@ -157,6 +162,8 @@ 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. + scale_factor = float(width) / base_width height = int(base_height * scale_factor) @@ -192,6 +199,7 @@ 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) @@ -208,7 +216,7 @@ # 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 = imp.resize((image_width, int(image_height * scale_factor))) imp.save(preview_filename) @@ -220,7 +228,7 @@ # 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 = im.resize((image_width, int(image_height * scale_factor))) im.save(output_filename)