PaletteOptimiser

Changeset

127:0d27eee1a2bb
2015-10-12 Paul Boddie raw files shortlog changelog graph Made more thorough fixes to the scaling of images.
optimiser.py (file)
     1.1 --- a/optimiser.py	Mon Oct 12 18:10:05 2015 +0200
     1.2 +++ b/optimiser.py	Mon Oct 12 20:45:09 2015 +0200
     1.3 @@ -67,12 +67,17 @@
     1.4          im = im.rotate(270)
     1.5  
     1.6      w, h = im.size
     1.7 -    if w > h:
     1.8 -        height = (width * h) / w
     1.9 +
    1.10 +    width_scale_factor = float(width) / w
    1.11 +    height_scale_factor = float(height) / h
    1.12 +    scale_factor = min(width_scale_factor, height_scale_factor)
    1.13 +
    1.14 +    if scale_factor < 1:
    1.15 +        width = int(scale_factor * w)
    1.16 +        height = int(scale_factor * h)
    1.17 +        return im.resize((width, height))
    1.18      else:
    1.19 -        width = (height * w) / h
    1.20 -
    1.21 -    return im.resize((width, height))
    1.22 +        return im
    1.23  
    1.24  def get_parameter(options, flag, conversion, default, missing):
    1.25  
    1.26 @@ -157,6 +162,8 @@
    1.27      number_of_colours = get_parameter(options, "-C", int, 4, 4)
    1.28      preserve_aspect_ratio = "-A" in options
    1.29  
    1.30 +    # Determine whether the height will need adjusting before conversion.
    1.31 +
    1.32      scale_factor = float(width) / base_width
    1.33      height = int(base_height * scale_factor)
    1.34  
    1.35 @@ -192,6 +199,7 @@
    1.36  
    1.37          if scale_factor != 1:
    1.38              im = im.resize((image_width, int(image_height / scale_factor)))
    1.39 +            image_width, image_height = im.size
    1.40  
    1.41          process_image(im, saturate, desaturate, darken, brighten)
    1.42  
    1.43 @@ -208,7 +216,7 @@
    1.44          # Scale images to a height determined by the aspect ratio.
    1.45  
    1.46          if preserve_aspect_ratio and scale_factor != 1:
    1.47 -            imp = imp.resize((image_width, image_height))
    1.48 +            imp = imp.resize((image_width, int(image_height * scale_factor)))
    1.49  
    1.50          imp.save(preview_filename)
    1.51  
    1.52 @@ -220,7 +228,7 @@
    1.53          # Scale images to a height determined by the aspect ratio.
    1.54  
    1.55          if preserve_aspect_ratio and scale_factor != 1:
    1.56 -            im = im.resize((image_width, image_height))
    1.57 +            im = im.resize((image_width, int(image_height * scale_factor)))
    1.58  
    1.59          im.save(output_filename)
    1.60