PaletteOptimiser

Changeset

132:c97a0dbd98d8
2015-10-12 Paul Boddie raw files shortlog changelog graph Separated scaling and width-stretching operations. simpleimage-shedskin snapshot-20160207
optimiser.py (file)
     1.1 --- a/optimiser.py	Mon Oct 12 20:46:03 2015 +0200
     1.2 +++ b/optimiser.py	Mon Oct 12 23:51:14 2015 +0200
     1.3 @@ -50,7 +50,7 @@
     1.4              im.putpixel((x, y), get_value(rgb))
     1.5      im.save("rgb%02d%02d%02d.png" % rgb)
     1.6  
     1.7 -def rotate_and_scale(exif, im, width, height, rotate):
     1.8 +def rotate_and_scale(exif, im, width, height, rotate, scale_factor):
     1.9  
    1.10      """
    1.11      Using the given 'exif' information, rotate and scale image 'im' given the
    1.12 @@ -64,14 +64,19 @@
    1.13  
    1.14      w, h = im.size
    1.15  
    1.16 -    width_scale_factor = float(width) / w
    1.17 +    # Get the relationship between the base width and the image width.
    1.18 +
    1.19 +    width_scale_factor = (width / scale_factor) / w
    1.20      height_scale_factor = float(height) / h
    1.21 -    scale_factor = min(width_scale_factor, height_scale_factor)
    1.22 +    min_scale_factor = min(width_scale_factor, height_scale_factor)
    1.23  
    1.24 -    if scale_factor < 1:
    1.25 -        width = int(scale_factor * w)
    1.26 -        height = int(scale_factor * h)
    1.27 +    if min_scale_factor < 1:
    1.28 +        width = int(min_scale_factor * w * scale_factor)
    1.29 +        height = int(min_scale_factor * h)
    1.30          return im.resize((width, height))
    1.31 +    elif scale_factor != 1:
    1.32 +        width = int(w * scale_factor)
    1.33 +        return im.resize((width, h))
    1.34      else:
    1.35          return im
    1.36  
    1.37 @@ -158,10 +163,9 @@
    1.38      number_of_colours = get_parameter(options, "-C", int, 4, 4)
    1.39      preserve_aspect_ratio = "-A" in options
    1.40  
    1.41 -    # Determine whether the height will need adjusting before conversion.
    1.42 +    # Determine any differing horizontal scale factor.
    1.43  
    1.44      scale_factor = float(width) / base_width
    1.45 -    height = int(base_height * scale_factor)
    1.46  
    1.47      # Preprocessing options that employ parameters.
    1.48  
    1.49 @@ -188,15 +192,9 @@
    1.50      if make_image or preview:
    1.51          exif = EXIF.process_file(open(input_filename))
    1.52          im = PIL.Image.open(input_filename).convert("RGB")
    1.53 -        im = rotate_and_scale(exif, im, width, height, rotate)
    1.54 +        im = rotate_and_scale(exif, im, width, height, rotate, scale_factor)
    1.55          image_width, image_height = im.size
    1.56  
    1.57 -        # Scale images to the appropriate height.
    1.58 -
    1.59 -        if scale_factor != 1:
    1.60 -            im = im.resize((image_width, int(image_height / scale_factor)))
    1.61 -            image_width, image_height = im.size
    1.62 -
    1.63          sim = SimpleImage(list(im.getdata()), im.size)
    1.64          process_image(sim, saturate, desaturate, darken, brighten)
    1.65          im.putdata(sim.getdata())