PaletteOptimiser

Changeset

121:649cbff60e7c
2015-10-12 Paul Boddie raw files shortlog changelog graph Fixed scaling calculations; added explicit support to preserve the aspect ratio. simpleimage
optimiser.py (file)
     1.1 --- a/optimiser.py	Sun Oct 11 23:29:34 2015 +0200
     1.2 +++ b/optimiser.py	Mon Oct 12 00:46:40 2015 +0200
     1.3 @@ -112,6 +112,8 @@
     1.4  
     1.5  -W - Indicate the output width (default is 320)
     1.6  -C - Number of colours per scanline (default is 4)
     1.7 +-A - Produce an output image with the same aspect ratio as the input
     1.8 +     (useful for previewing)
     1.9  
    1.10  -s - Saturate the input image (optional float, 1.0 if unspecified)
    1.11  -d - Desaturate the input image (optional float, 1.0 if unspecified)
    1.12 @@ -133,8 +135,8 @@
    1.13  """ % split(sys.argv[0])[1]
    1.14          sys.exit(1)
    1.15  
    1.16 -    base_width = 320
    1.17 -    height = 256
    1.18 +    base_width = width = 320
    1.19 +    base_height = height = 256
    1.20  
    1.21      input_filename, output_filename = sys.argv[1:3]
    1.22      basename, ext = splitext(output_filename)
    1.23 @@ -146,6 +148,10 @@
    1.24  
    1.25      width = get_parameter(options, "-W", int, base_width, base_width)
    1.26      number_of_colours = get_parameter(options, "-C", int, 4, 4)
    1.27 +    preserve_aspect_ratio = "-A" in options
    1.28 +
    1.29 +    scale_factor = float(width) / base_width
    1.30 +    height = int(base_height * scale_factor)
    1.31  
    1.32      # Preprocessing options that employ parameters.
    1.33  
    1.34 @@ -169,12 +175,13 @@
    1.35      if make_image or preview:
    1.36          exif = EXIF.process_file(open(input_filename))
    1.37          im = PIL.Image.open(input_filename).convert("RGB")
    1.38 -        im = rotate_and_scale(exif, im, base_width, height, rotate)
    1.39 +        im = rotate_and_scale(exif, im, width, height, rotate)
    1.40 +        image_width, image_height = im.size
    1.41  
    1.42 -        # Scale images to the appropriate width.
    1.43 +        # Scale images to the appropriate height.
    1.44  
    1.45 -        if width != base_width:
    1.46 -            im = im.resize((width, height))
    1.47 +        if scale_factor != 1:
    1.48 +            im = im.resize((image_width, int(image_height / scale_factor)))
    1.49  
    1.50          process_image(im, saturate, desaturate, darken, brighten)
    1.51  
    1.52 @@ -183,16 +190,28 @@
    1.53      if preview:
    1.54          imp = im.copy()
    1.55          if half_resolution_preview:
    1.56 -            imp = imp.resize((width / 2, height))
    1.57 +            imp = imp.resize((image_width / 2, image_height))
    1.58          convert_image(imp, 8)
    1.59          if half_resolution_preview:
    1.60 -            imp = imp.resize((width, height))
    1.61 +            imp = imp.resize((image_width, image_height))
    1.62 +
    1.63 +        # Scale images to a height determined by the aspect ratio.
    1.64 +
    1.65 +        if preserve_aspect_ratio and scale_factor != 1:
    1.66 +            imp = imp.resize((image_width, image_height))
    1.67 +
    1.68          imp.save(preview_filename)
    1.69  
    1.70      # Generate an output image if requested.
    1.71  
    1.72      if make_image:
    1.73          convert_image(im, number_of_colours, least_error)
    1.74 +
    1.75 +        # Scale images to a height determined by the aspect ratio.
    1.76 +
    1.77 +        if preserve_aspect_ratio and scale_factor != 1:
    1.78 +            im = im.resize((image_width, image_height))
    1.79 +
    1.80          im.save(output_filename)
    1.81  
    1.82      # Verify the output image (which may be loaded) if requested.