PaletteOptimiser

Changeset

122:dac076310b26
2015-10-12 Paul Boddie raw files shortlog changelog graph Fixed scaling calculations; added explicit support to preserve the aspect ratio. simpleimage-shedskin snapshot-20151012
optimiser.py (file)
     1.1 --- a/optimiser.py	Sun Oct 11 23:31:25 2015 +0200
     1.2 +++ b/optimiser.py	Mon Oct 12 00:48:50 2015 +0200
     1.3 @@ -108,6 +108,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 @@ -129,8 +131,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 @@ -142,6 +144,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 @@ -165,12 +171,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          sim = SimpleImage(list(im.getdata()), im.size)
    1.51          process_image(sim, saturate, desaturate, darken, brighten)
    1.52 @@ -181,12 +188,18 @@
    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          sim = SimpleImage(list(imp.getdata()), imp.size)
    1.59          convert_image(sim, 8)
    1.60          imp.putdata(sim.getdata())
    1.61          if half_resolution_preview:
    1.62 -            imp = imp.resize((width, height))
    1.63 +            imp = imp.resize((image_width, image_height))
    1.64 +
    1.65 +        # Scale images to a height determined by the aspect ratio.
    1.66 +
    1.67 +        if preserve_aspect_ratio and scale_factor != 1:
    1.68 +            imp = imp.resize((image_width, image_height))
    1.69 +
    1.70          imp.save(preview_filename)
    1.71  
    1.72      # Generate an output image if requested.
    1.73 @@ -195,6 +208,12 @@
    1.74          sim = SimpleImage(list(im.getdata()), im.size)
    1.75          convert_image(sim, number_of_colours, least_error)
    1.76          im.putdata(sim.getdata())
    1.77 +
    1.78 +        # Scale images to a height determined by the aspect ratio.
    1.79 +
    1.80 +        if preserve_aspect_ratio and scale_factor != 1:
    1.81 +            im = im.resize((image_width, image_height))
    1.82 +
    1.83          im.save(output_filename)
    1.84  
    1.85      # Verify the output image (which may be loaded) if requested.