PaletteOptimiser

Change of optimiser.py

26:cd815af3effc
optimiser.py
     1.1 --- a/optimiser.py	Thu Oct 01 12:50:55 2015 +0200
     1.2 +++ b/optimiser.py	Thu Oct 01 18:39:59 2015 +0200
     1.3 @@ -1,7 +1,7 @@
     1.4  #!/usr/bin/env python
     1.5  
     1.6 -from itertools import combinations
     1.7  from random import randint
     1.8 +from os.path import extsep, splitext
     1.9  import EXIF
    1.10  import PIL.Image
    1.11  import math
    1.12 @@ -50,8 +50,8 @@
    1.13      return start, end, f
    1.14  
    1.15  def choose(seq, f):
    1.16 -    last = int(seq * math.sqrt(f))
    1.17 -    current = int((seq + 1) * math.sqrt(f))
    1.18 +    last = int(seq * f)
    1.19 +    current = int((seq + 1) * f)
    1.20      return last != current
    1.21  
    1.22  def get_value(xy, rgb, width, height, values=None):
    1.23 @@ -116,6 +116,10 @@
    1.24      height = 256
    1.25  
    1.26      input_filename, output_filename = sys.argv[1:3]
    1.27 +    basename, ext = splitext(output_filename)
    1.28 +    preview_filename = extsep.join([basename + "_preview", ext])
    1.29 +
    1.30 +    preview = "-p" in sys.argv[3:]
    1.31      rotate = "-r" in sys.argv[3:]
    1.32      saturate = sys.argv[3:].count("-s")
    1.33      desaturate = sys.argv[3:].count("-d")
    1.34 @@ -124,6 +128,9 @@
    1.35      im = PIL.Image.open(input_filename)
    1.36      im = rotate_and_scale(im, width, height, rotate)
    1.37  
    1.38 +    if preview:
    1.39 +        im_preview = im.copy()
    1.40 +
    1.41      width, height = im.size
    1.42  
    1.43      colours = []
    1.44 @@ -147,10 +154,16 @@
    1.45              else:
    1.46                  c[value] += 1
    1.47  
    1.48 +            if preview:
    1.49 +                im_preview.putpixel((x, y), value)
    1.50 +
    1.51          c = [(n, value) for value, n in c.items()]
    1.52          c.sort(reverse=True)
    1.53          colours.append(c)
    1.54  
    1.55 +    if preview:
    1.56 +        im_preview.save(preview_filename)
    1.57 +
    1.58      for y, c in enumerate(colours):
    1.59          most = [value for n, value in c[:4]]
    1.60          least = [value for n, value in c[4:]]