PaletteOptimiser

dither.py

31:244292b4816c
2015-10-02 Paul Boddie Use a proper main program!
     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 if __name__ == "__main__":    21     im = PIL.Image.open(sys.argv[1])    22     im2 = dither(im)    23     im2.save(sys.argv[2])    24     25 # vim: tabstop=4 expandtab shiftwidth=4