# HG changeset patch # User Paul Boddie # Date 1444675563 -7200 # Node ID 62cdce59435f584fb310606bf622fff0657c8d4e # Parent e51a3f1dd41316e1bb06d69221622ca726548b22# Parent 93aaf33edc7325b6e0c446e30078c9a8035b82fa Made more thorough fixes to the scaling of images. diff -r e51a3f1dd413 -r 62cdce59435f optimiser.py --- a/optimiser.py Mon Oct 12 18:10:53 2015 +0200 +++ b/optimiser.py Mon Oct 12 20:46:03 2015 +0200 @@ -63,12 +63,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): @@ -153,6 +158,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) @@ -188,6 +195,7 @@ 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) @@ -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) @@ -222,7 +230,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)