PaletteOptimiser

Changeset

111:680956899c9e
2015-10-11 Paul Boddie raw files shortlog changelog graph Replaced "for" loops with "while" loops for extra performance. simpleimage
optimiser.py (file)
     1.1 --- a/optimiser.py	Sun Oct 11 15:56:31 2015 +0200
     1.2 +++ b/optimiser.py	Sun Oct 11 18:50:55 2015 +0200
     1.3 @@ -182,7 +182,8 @@
     1.4  
     1.5      width, height = im.size
     1.6      c = {}
     1.7 -    for x in range(0, width):
     1.8 +    x = 0
     1.9 +    while x < width:
    1.10          rgb = im.getpixel((x, y))
    1.11  
    1.12          # Sum the colour probabilities.
    1.13 @@ -193,6 +194,8 @@
    1.14              else:
    1.15                  c[value] += f
    1.16  
    1.17 +        x += 1
    1.18 +
    1.19      d = [(n/width, value) for value, n in c.items()]
    1.20      d.sort(reverse=True)
    1.21      return d
    1.22 @@ -232,9 +235,13 @@
    1.23  
    1.24      size = 64
    1.25      im = PIL.Image.new("RGB", (size, size))
    1.26 -    for y in range(0, size):
    1.27 -        for x in range(0, size):
    1.28 +    y = 0
    1.29 +    while y < height:
    1.30 +        x = 0
    1.31 +        while x < width:
    1.32              im.putpixel((x, y), get_value(rgb))
    1.33 +            x += 1
    1.34 +        y += 1
    1.35      im.save("rgb%02d%02d%02d.png" % rgb)
    1.36  
    1.37  def rotate_and_scale(exif, im, width, height, rotate):
    1.38 @@ -267,12 +274,16 @@
    1.39  
    1.40      width, height = im.size
    1.41  
    1.42 -    for y in range(0, height):
    1.43 +    y = 0
    1.44 +    while y < height:
    1.45          l = set()
    1.46 -        for x in range(0, width):
    1.47 +        x = 0
    1.48 +        while x < width:
    1.49              l.add(im.getpixel((x, y)))
    1.50 +            x += 1
    1.51          if len(l) > colours:
    1.52              return (y, l)
    1.53 +        y += 1
    1.54      return None
    1.55  
    1.56  def process_image(pim, saturate, desaturate, darken, brighten):
    1.57 @@ -286,14 +297,18 @@
    1.58      im = SimpleImage(list(pim.getdata()), pim.size)
    1.59  
    1.60      if saturate or desaturate or darken or brighten:
    1.61 -        for y in range(0, height):
    1.62 -            for x in range(0, width):
    1.63 +        y = 0
    1.64 +        while y < height:
    1.65 +            x = 0
    1.66 +            while x < width:
    1.67                  rgb = im.getpixel((x, y))
    1.68                  if saturate or desaturate:
    1.69                      rgb = saturate_rgb(rgb, saturate and 0.5 / saturate or 2 * desaturate)
    1.70                  if darken or brighten:
    1.71                      rgb = amplify_rgb(rgb, brighten and 0.5 / brighten or 2 * darken)
    1.72                  im.putpixel((x, y), rgb)
    1.73 +                x += 1
    1.74 +            y += 1
    1.75  
    1.76      pim.putdata(im.getdata())
    1.77  
    1.78 @@ -304,7 +319,8 @@
    1.79      width, height = pim.size
    1.80      im = SimpleImage(list(pim.getdata()), pim.size)
    1.81  
    1.82 -    for y in range(0, height):
    1.83 +    y = 0
    1.84 +    while y < height:
    1.85          c = get_colours(im, y)
    1.86  
    1.87          suggestions = []
    1.88 @@ -313,11 +329,13 @@
    1.89              most = [value for f, value in l]
    1.90              missing = 0
    1.91  
    1.92 -            for x in range(0, width):
    1.93 +            x = 0
    1.94 +            while x < width:
    1.95                  rgb = im.getpixel((x, y))
    1.96                  value = get_value(rgb, most, True)
    1.97                  if value is None:
    1.98                      missing += 1
    1.99 +                x += 1
   1.100  
   1.101              if not missing:
   1.102                  break # use this combination
   1.103 @@ -329,7 +347,8 @@
   1.104              suggestions.sort()
   1.105              most = [value for f, value in suggestions[0][1]] # get the combination
   1.106  
   1.107 -        for x in range(0, width):
   1.108 +        x = 0
   1.109 +        while x < width:
   1.110              rgb = im.getpixel((x, y))
   1.111              value = get_value(rgb, most)
   1.112              im.putpixel((x, y), value)
   1.113 @@ -352,6 +371,10 @@
   1.114                      )
   1.115                  im.putpixel((x, y+1), rgbn)
   1.116  
   1.117 +            x += 1
   1.118 +
   1.119 +        y += 1
   1.120 +
   1.121      pim.putdata(im.getdata())
   1.122  
   1.123  def get_parameter(options, flag, conversion, default, missing):