I did this:
HEXATABLE = {}
for key, value in COLORTABLE.items():
HEXATABLE[key] = rgb2hex(value)
def rgb2hex(rgb):
return (rgb[0] << 16) + (rgb[1] << 8) + rgb[2];
def hex2rgb(hexcolor):
r = ( hexcolor >> 16 ) & 0xFF;
g = ( hexcolor >> 8 ) & 0xFF;
b = hexcolor & 0xFF;
return r, g, b
def takeClosest(num,collection):
return min(collection,key=lambda x:abs(x-num))
for x in range(img.get_width()):
for y in range(img.get_height()):
color = rgb2hex(tuple(img.get_at((x, y))))
newcolor = hex2rgb(takeClosest(color, HEXATABLE.values()))
Cobbled together out of chunks of code from StackOverflow.