PaletteOptimiser

Changeset

112:aaa2938a0ac2
2015-10-11 Paul Boddie raw files shortlog changelog graph Replaced "for" loops with "while" loops for extra performance. simpleimage-shedskin
optimiser.py (file) optimiserlib.py (file)
     2.1 --- a/optimiserlib.py	Sun Oct 11 18:07:59 2015 +0200
     2.2 +++ b/optimiserlib.py	Sun Oct 11 19:12:15 2015 +0200
     2.3 @@ -181,7 +181,8 @@
     2.4  
     2.5      width, height = im.size
     2.6      c = {}
     2.7 -    for x in range(0, width):
     2.8 +    x = 0
     2.9 +    while x < width:
    2.10          rgb = im.getpixel((x, y))
    2.11  
    2.12          # Sum the colour probabilities.
    2.13 @@ -192,6 +193,8 @@
    2.14              else:
    2.15                  c[value] += f
    2.16  
    2.17 +        x += 1
    2.18 +
    2.19      d = [(n/width, value) for value, n in c.items()]
    2.20      d.sort(reverse=True)
    2.21      return d
    2.22 @@ -222,12 +225,16 @@
    2.23  
    2.24      width, height = im.size
    2.25  
    2.26 -    for y in range(0, height):
    2.27 +    y = 0
    2.28 +    while y < height:
    2.29          l = set()
    2.30 -        for x in range(0, width):
    2.31 +        x = 0
    2.32 +        while x < width:
    2.33              l.add(im.getpixel((x, y)))
    2.34 +            x += 1
    2.35          if len(l) > colours:
    2.36              return (y, l)
    2.37 +        y += 1
    2.38      return None
    2.39  
    2.40  def process_image(im, saturate, desaturate, darken, brighten):
    2.41 @@ -240,14 +247,18 @@
    2.42      width, height = im.size
    2.43  
    2.44      if saturate or desaturate or darken or brighten:
    2.45 -        for y in range(0, height):
    2.46 -            for x in range(0, width):
    2.47 +        y = 0
    2.48 +        while y < height:
    2.49 +            x = 0
    2.50 +            while x < width:
    2.51                  rgb = im.getpixel((x, y))
    2.52                  if saturate or desaturate:
    2.53                      rgb = saturate_rgb(rgb, saturate and 0.5 / saturate or 2.0 * desaturate)
    2.54                  if darken or brighten:
    2.55                      rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2.0 * darken)
    2.56                  im.putpixel((x, y), rgb)
    2.57 +                x += 1
    2.58 +            y += 1
    2.59  
    2.60  def convert_image(im, colours):
    2.61  
    2.62 @@ -255,7 +266,8 @@
    2.63  
    2.64      width, height = im.size
    2.65  
    2.66 -    for y in range(0, height):
    2.67 +    y = 0
    2.68 +    while y < height:
    2.69          c = get_colours(im, y)
    2.70  
    2.71          suggestions = []
    2.72 @@ -264,11 +276,13 @@
    2.73              most = [value for f, value in l]
    2.74              missing = 0
    2.75  
    2.76 -            for x in range(0, width):
    2.77 +            x = 0
    2.78 +            while x < width:
    2.79                  rgb = im.getpixel((x, y))
    2.80                  value = get_value(rgb, most, True)
    2.81                  if value is None:
    2.82                      missing += 1
    2.83 +                x += 1
    2.84  
    2.85              if not missing:
    2.86                  break # use this combination
    2.87 @@ -280,7 +294,8 @@
    2.88              suggestions.sort()
    2.89              most = [value for f, value in suggestions[0][1]] # get the combination
    2.90  
    2.91 -        for x in range(0, width):
    2.92 +        x = 0
    2.93 +        while x < width:
    2.94              rgb = im.getpixel((x, y))
    2.95              value = get_value(rgb, most)
    2.96              im.putpixel((x, y), value)
    2.97 @@ -303,6 +318,10 @@
    2.98                      )
    2.99                  im.putpixel((x, y+1), rgbn)
   2.100  
   2.101 +            x += 1
   2.102 +
   2.103 +        y += 1
   2.104 +
   2.105  class SimpleImage:
   2.106  
   2.107      "An image behaving like PIL.Image."