PaletteOptimiser

dither.py

29:21c5d47536ea
2015-10-02 Paul Boddie Added experiments with PIL's dithering/quantizing, summing colour probabilities, finding the "best" combinations where more than four colours are required. Removed the preview image saving.
     1 #!/usr/bin/env python     2      3 import PIL.Image     4 import sys     5      6 def get_palette():     7     l = []     8     for i in range(0, 8):     9         r = ((i / 4) % 2) * 255; g = ((i / 2) % 2) * 255; b = (i % 2) * 255    10         for j in range(0, 32):    11             l.extend((r, g, b))    12     13     imp = PIL.Image.new("P", (1, 1))    14     imp.putpalette(l)    15     return imp    16     17 def dither(im, imp=None):    18     return im.quantize(palette=imp or get_palette())    19     20 im = PIL.Image.open(sys.argv[1])    21 im2 = dither(im)    22 im2.save(sys.argv[2])    23     24 # vim: tabstop=4 expandtab shiftwidth=4