PaletteOptimiser

Change of optimiserlib.py

91:4e37f510ccfd
optimiserlib.py simpleimage-shedskin
     1.1 --- a/optimiserlib.py	Sat Oct 10 16:48:46 2015 +0200
     1.2 +++ b/optimiserlib.py	Sat Oct 10 18:13:07 2015 +0200
     1.3 @@ -270,6 +270,50 @@
     1.4  
     1.5      return imp
     1.6  
     1.7 +def convert_image(im):
     1.8 +
     1.9 +    "Convert image 'im' to an appropriate output representation."
    1.10 +
    1.11 +    width, height = im.size
    1.12 +
    1.13 +    for y in range(0, height):
    1.14 +        c = get_colours(im, y)
    1.15 +
    1.16 +        for l in get_combinations(c, 4):
    1.17 +            most = [value for f, value in l]
    1.18 +            for x in range(0, width):
    1.19 +                rgb = im.getpixel((x, y))
    1.20 +                value = get_value(rgb, most, True)
    1.21 +                if value is None:
    1.22 +                    break # try next combination
    1.23 +            else:
    1.24 +                break # use this combination
    1.25 +        else:
    1.26 +            most = [value for f, value in c[:4]] # use the first four
    1.27 +
    1.28 +        for x in range(0, width):
    1.29 +            rgb = im.getpixel((x, y))
    1.30 +            value = get_value(rgb, most)
    1.31 +            im.putpixel((x, y), value)
    1.32 +
    1.33 +            if x < width - 1:
    1.34 +                rgbn = im.getpixel((x+1, y))
    1.35 +                rgbn = (
    1.36 +                    clip(rgbn[0] + (rgb[0] - value[0]) / 4.0),
    1.37 +                    clip(rgbn[1] + (rgb[1] - value[1]) / 4.0),
    1.38 +                    clip(rgbn[2] + (rgb[2] - value[2]) / 4.0)
    1.39 +                    )
    1.40 +                im.putpixel((x+1, y), rgbn)
    1.41 +
    1.42 +            if y < height - 1:
    1.43 +                rgbn = im.getpixel((x, y+1))
    1.44 +                rgbn = (
    1.45 +                    clip(rgbn[0] + (rgb[0] - value[0]) / 2.0),
    1.46 +                    clip(rgbn[1] + (rgb[1] - value[1]) / 2.0),
    1.47 +                    clip(rgbn[2] + (rgb[2] - value[2]) / 2.0)
    1.48 +                    )
    1.49 +                im.putpixel((x, y+1), rgbn)
    1.50 +
    1.51  class SimpleImage:
    1.52  
    1.53      "An image behaving like PIL.Image."
    1.54 @@ -316,5 +360,6 @@
    1.55  
    1.56      process_image(im, 1.0, 0.0, 1.0, 0.0)
    1.57      preview_image(im, False)
    1.58 +    convert_image(im)
    1.59  
    1.60  # vim: tabstop=4 expandtab shiftwidth=4