PaletteOptimiser

Change of optimiser.py

75:429cc2ef9b9f
optimiser.py simpleimage
     1.1 --- a/optimiser.py	Fri Oct 09 22:16:49 2015 +0200
     1.2 +++ b/optimiser.py	Sat Oct 10 14:41:25 2015 +0200
     1.3 @@ -188,9 +188,9 @@
     1.4              else:
     1.5                  c[value] += f
     1.6  
     1.7 -    c = [(n/width, value) for value, n in c.items()]
     1.8 -    c.sort(reverse=True)
     1.9 -    return c
    1.10 +    d = [(n/width, value) for value, n in c.items()]
    1.11 +    d.sort(reverse=True)
    1.12 +    return d
    1.13  
    1.14  def get_combinations(c, n):
    1.15  
    1.16 @@ -270,14 +270,15 @@
    1.17              return (y, l)
    1.18      return None
    1.19  
    1.20 -def process_image(im, saturate, desaturate, darken, brighten):
    1.21 +def process_image(pim, saturate, desaturate, darken, brighten):
    1.22  
    1.23      """
    1.24 -    Process image 'im' using the given options: 'saturate', 'desaturate',
    1.25 +    Process image 'pim' using the given options: 'saturate', 'desaturate',
    1.26      'darken', 'brighten'.
    1.27      """
    1.28  
    1.29 -    width, height = im.size
    1.30 +    width, height = pim.size
    1.31 +    im = SimpleImage(list(pim.getdata()), pim.size)
    1.32  
    1.33      if saturate or desaturate or darken or brighten:
    1.34          for y in range(0, height):
    1.35 @@ -289,30 +290,34 @@
    1.36                      rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2 * darken)
    1.37                  im.putpixel((x, y), rgb)
    1.38  
    1.39 -def preview_image(im, half_resolution_preview=False):
    1.40 +    pim.putdata(im.getdata())
    1.41  
    1.42 -    "Return a preview copy of image 'im'."
    1.43 +def preview_image(pim, half_resolution_preview=False):
    1.44  
    1.45 -    width, height = im.size
    1.46 +    "Return a preview copy of image 'pim'."
    1.47  
    1.48 -    imp = im.copy()
    1.49 +    width, height = pim.size
    1.50 +    imp = pim.copy()
    1.51 +    im = SimpleImage(list(pim.getdata()), pim.size)
    1.52      step = half_resolution_preview and 2 or 1
    1.53  
    1.54      for y in range(0, height):
    1.55 -        for x in range(0, width, step):
    1.56 -            rgb = imp.getpixel((x, y))
    1.57 +        for x in range(0, width):
    1.58 +            rgb = im.getpixel((x, y))
    1.59              value = get_value(rgb)
    1.60 -            imp.putpixel((x, y), value)
    1.61 +            im.putpixel((x, y), value)
    1.62              if half_resolution_preview:
    1.63 -                imp.putpixel((x+1, y), value)
    1.64 +                im.putpixel((x+1, y), value)
    1.65  
    1.66 +    imp.putdata(im.getdata())
    1.67      return imp
    1.68  
    1.69 -def convert_image(im):
    1.70 +def convert_image(pim):
    1.71  
    1.72 -    "Convert image 'im' to an appropriate output representation."
    1.73 +    "Convert image 'pim' to an appropriate output representation."
    1.74  
    1.75 -    width, height = im.size
    1.76 +    width, height = pim.size
    1.77 +    im = SimpleImage(list(pim.getdata()), pim.size)
    1.78  
    1.79      for y in range(0, height):
    1.80          c = get_colours(im, y)
    1.81 @@ -344,6 +349,8 @@
    1.82                  rgbn = tuple(map(lambda i: clip(i[0] + (i[1] - i[2]) / 2.0), zip(rgbn, rgb, value)))
    1.83                  im.putpixel((x, y+1), rgbn)
    1.84  
    1.85 +    pim.putdata(im.getdata())
    1.86 +
    1.87  def get_float(options, flag):
    1.88      try:
    1.89          i = options.index(flag)
    1.90 @@ -354,6 +361,28 @@
    1.91      except ValueError:
    1.92          return 0.0
    1.93  
    1.94 +class SimpleImage:
    1.95 +
    1.96 +    "An image behaving like PIL.Image."
    1.97 +
    1.98 +    def __init__(self, data, size):
    1.99 +        self.data = data
   1.100 +        self.width, self.height = self.size = size
   1.101 +
   1.102 +    def copy(self):
   1.103 +        return SimpleImage(self.data[:], self.size)
   1.104 +
   1.105 +    def getpixel(self, xy):
   1.106 +        x, y = xy
   1.107 +        return self.data[y * self.width + x]
   1.108 +
   1.109 +    def putpixel(self, xy, value):
   1.110 +        x, y = xy
   1.111 +        self.data[y * self.width + x] = value
   1.112 +
   1.113 +    def getdata(self):
   1.114 +        return self.data
   1.115 +
   1.116  # Main program.
   1.117  
   1.118  if __name__ == "__main__":