#!/usr/bin/env python
"""
imgcheck - check a sequence of images for exiguous colors

usage: imgcheck baseframe imgframe...

In a sequence of images, treat the first as a baseframe and the rest
as animation images.  Report on colors not present in the baseframe.

The Python Imaging Library must be available for this tool to work
"""

import sys, Image

# Extract a color table list from the images
colortables = []
for filename in sys.argv[1:]:
    img = Image.open(filename).convert("RGB")
    colortable = img.getcolors()
    if colortable:
        colortables.append([filename, colortable])
    else:
        print "imgcheck: %s has no color table" % filename
    #img.close()

# Perform cxolor table subtraction
basecolors = map(lambda (n, rgb): rgb, colortables[0][1])
basecolors.sort()
subtracted = []
for i in range(1, len(colortables)):
    exiguous = []
    for (n, rgb) in colortables[i][1]:
        if rgb not in basecolors:
            exiguous.append((n, rgb))
    exiguous.sort(lambda (an, argb), (bn, brgb): cmp(argb, brgb))
    subtracted.append((colortables[i][0], exiguous))

print "Base colors:"
print basecolors
for (filename, colors) in subtracted:
    print filename + ":"
    for (n, rgb) in colors:
        print repr(rgb) + " * " + repr(n)
