PaletteOptimiser

Changeset

84:86d83f74ef9a
2015-10-10 Paul Boddie raw files shortlog changelog graph Moved the get_combinations function into the extension module. simpleimage-shedskin
optimiser.py (file) optimiserlib.py (file)
     1.1 --- a/optimiser.py	Sat Oct 10 15:36:48 2015 +0200
     1.2 +++ b/optimiser.py	Sat Oct 10 15:59:38 2015 +0200
     1.3 @@ -24,7 +24,6 @@
     1.4  from os.path import split, splitext
     1.5  import EXIF
     1.6  import PIL.Image
     1.7 -import itertools
     1.8  import sys
     1.9  
    1.10  # Image operations.
    1.11 @@ -50,22 +49,6 @@
    1.12      d.sort(reverse=True)
    1.13      return d
    1.14  
    1.15 -def get_combinations(c, n):
    1.16 -
    1.17 -    """
    1.18 -    Get combinations of colours from 'c' of size 'n' in decreasing order of
    1.19 -    probability.
    1.20 -    """
    1.21 -
    1.22 -    all = []
    1.23 -    for l in itertools.combinations(c, n):
    1.24 -        total = 0
    1.25 -        for f, value in l:
    1.26 -            total += f
    1.27 -        all.append((total, l))
    1.28 -    all.sort(reverse=True)
    1.29 -    return [l for total, l in all]
    1.30 -
    1.31  def test():
    1.32  
    1.33      "Generate slices of the colour cube."
     2.1 --- a/optimiserlib.py	Sat Oct 10 15:36:48 2015 +0200
     2.2 +++ b/optimiserlib.py	Sat Oct 10 15:59:38 2015 +0200
     2.3 @@ -21,6 +21,7 @@
     2.4  """
     2.5  
     2.6  from random import random, randrange
     2.7 +import itertools
     2.8  
     2.9  corners = [
    2.10      (0, 0, 0), (255, 0, 0), (0, 255, 0), (255, 255, 0),
    2.11 @@ -172,6 +173,24 @@
    2.12  def amplify_value(x, exp):
    2.13      return int(pow(x / 255.0, exp) * 255.0)
    2.14  
    2.15 +# Image operations.
    2.16 +
    2.17 +def get_combinations(c, n):
    2.18 +
    2.19 +    """
    2.20 +    Get combinations of colours from 'c' of size 'n' in decreasing order of
    2.21 +    probability.
    2.22 +    """
    2.23 +
    2.24 +    all = []
    2.25 +    for l in itertools.combinations(c, n):
    2.26 +        total = 0
    2.27 +        for f, value in l:
    2.28 +            total += f
    2.29 +        all.append((total, l))
    2.30 +    all.sort(reverse=True)
    2.31 +    return [l for total, l in all]
    2.32 +
    2.33  # Exercise functions for Shedskin.
    2.34  
    2.35  if __name__ == "__main__":
    2.36 @@ -182,5 +201,6 @@
    2.37      get_value(rgb, [(255, 255, 255), (255, 0, 0), (255, 255, 0), (0, 0, 0)])
    2.38      combine([(1.0, (255, 0, 0)), (0.0, (0, 0, 0))])
    2.39      clip(200.0)
    2.40 +    get_combinations([(0.5, (255, 0, 0)), (0.25, (255, 255, 0)), (0.25, (0, 0, 0))], 2)
    2.41  
    2.42  # vim: tabstop=4 expandtab shiftwidth=4