1.1 --- a/optimiser.py Sat Oct 10 16:48:46 2015 +0200
1.2 +++ b/optimiser.py Sat Oct 10 18:13:07 2015 +0200
1.3 @@ -72,53 +72,6 @@
1.4
1.5 return im.resize((width, height))
1.6
1.7 -def convert_image(pim):
1.8 -
1.9 - "Convert image 'pim' to an appropriate output representation."
1.10 -
1.11 - width, height = pim.size
1.12 - im = SimpleImage(list(pim.getdata()), pim.size)
1.13 -
1.14 - for y in range(0, height):
1.15 - c = get_colours(im, y)
1.16 -
1.17 - for l in get_combinations(c, 4):
1.18 - most = [value for f, value in l]
1.19 - for x in range(0, width):
1.20 - rgb = im.getpixel((x, y))
1.21 - value = get_value(rgb, most, True)
1.22 - if value is None:
1.23 - break # try next combination
1.24 - else:
1.25 - break # use this combination
1.26 - else:
1.27 - most = [value for f, value in c[:4]] # use the first four
1.28 -
1.29 - for x in range(0, width):
1.30 - rgb = im.getpixel((x, y))
1.31 - value = get_value(rgb, most)
1.32 - im.putpixel((x, y), value)
1.33 -
1.34 - if x < width - 1:
1.35 - rgbn = im.getpixel((x+1, y))
1.36 - rgbn = (
1.37 - clip(rgbn[0] + (rgb[0] - value[0]) / 4.0),
1.38 - clip(rgbn[1] + (rgb[1] - value[1]) / 4.0),
1.39 - clip(rgbn[2] + (rgb[2] - value[2]) / 4.0)
1.40 - )
1.41 - im.putpixel((x+1, y), rgbn)
1.42 -
1.43 - if y < height - 1:
1.44 - rgbn = im.getpixel((x, y+1))
1.45 - rgbn = (
1.46 - clip(rgbn[0] + (rgb[0] - value[0]) / 2.0),
1.47 - clip(rgbn[1] + (rgb[1] - value[1]) / 2.0),
1.48 - clip(rgbn[2] + (rgb[2] - value[2]) / 2.0)
1.49 - )
1.50 - im.putpixel((x, y+1), rgbn)
1.51 -
1.52 - pim.putdata(im.getdata())
1.53 -
1.54 def get_float(options, flag):
1.55 try:
1.56 i = options.index(flag)
1.57 @@ -208,7 +161,9 @@
1.58 # Generate an output image if requested.
1.59
1.60 if make_image:
1.61 - convert_image(im)
1.62 + sim = SimpleImage(list(im.getdata()), im.size)
1.63 + convert_image(sim)
1.64 + im.putdata(sim.getdata())
1.65 im.save(output_filename)
1.66
1.67 # Verify the output image (which may be loaded) if requested.
2.1 --- a/optimiserlib.py Sat Oct 10 16:48:46 2015 +0200
2.2 +++ b/optimiserlib.py Sat Oct 10 18:13:07 2015 +0200
2.3 @@ -270,6 +270,50 @@
2.4
2.5 return imp
2.6
2.7 +def convert_image(im):
2.8 +
2.9 + "Convert image 'im' to an appropriate output representation."
2.10 +
2.11 + width, height = im.size
2.12 +
2.13 + for y in range(0, height):
2.14 + c = get_colours(im, y)
2.15 +
2.16 + for l in get_combinations(c, 4):
2.17 + most = [value for f, value in l]
2.18 + for x in range(0, width):
2.19 + rgb = im.getpixel((x, y))
2.20 + value = get_value(rgb, most, True)
2.21 + if value is None:
2.22 + break # try next combination
2.23 + else:
2.24 + break # use this combination
2.25 + else:
2.26 + most = [value for f, value in c[:4]] # use the first four
2.27 +
2.28 + for x in range(0, width):
2.29 + rgb = im.getpixel((x, y))
2.30 + value = get_value(rgb, most)
2.31 + im.putpixel((x, y), value)
2.32 +
2.33 + if x < width - 1:
2.34 + rgbn = im.getpixel((x+1, y))
2.35 + rgbn = (
2.36 + clip(rgbn[0] + (rgb[0] - value[0]) / 4.0),
2.37 + clip(rgbn[1] + (rgb[1] - value[1]) / 4.0),
2.38 + clip(rgbn[2] + (rgb[2] - value[2]) / 4.0)
2.39 + )
2.40 + im.putpixel((x+1, y), rgbn)
2.41 +
2.42 + if y < height - 1:
2.43 + rgbn = im.getpixel((x, y+1))
2.44 + rgbn = (
2.45 + clip(rgbn[0] + (rgb[0] - value[0]) / 2.0),
2.46 + clip(rgbn[1] + (rgb[1] - value[1]) / 2.0),
2.47 + clip(rgbn[2] + (rgb[2] - value[2]) / 2.0)
2.48 + )
2.49 + im.putpixel((x, y+1), rgbn)
2.50 +
2.51 class SimpleImage:
2.52
2.53 "An image behaving like PIL.Image."
2.54 @@ -316,5 +360,6 @@
2.55
2.56 process_image(im, 1.0, 0.0, 1.0, 0.0)
2.57 preview_image(im, False)
2.58 + convert_image(im)
2.59
2.60 # vim: tabstop=4 expandtab shiftwidth=4