#!/usr/bin/env python

from PIL import Image
import numpy as np
im = Image.open("ARX_N0R_0.gif")
print im
palette = im.getpalette()
# make a numpy array out of that, too:
p = np.array(palette, dtype=np.uint8).reshape(-1,3)

# make an array out of the image:
a = np.asarray(im).copy()
print "it is a ", a.shape, "shaped array of:", a.dtype

print a[0,:]

indices = set(a.flat)
print "The indices used are:", indices

#We only care about the part of the palette that is used, so I'll jsut show the first 20
print p[:20] # note that's how you can do a LUT

# It look like the noise color is index-1 (2nd) color in the palette:
noise_color = 1
# white is index 13:
white = 13
# replace the noise color with white 
a[a==noise_color] = white

# you can see that 1 is no longer used:
print "The indices now used are:"
print set(a.flat)

im2 = Image.fromarray(a, mode='P')
print im2
# but it does not have a pallette:
print im2.palette
# give it the same palette
im2.putpalette(im.getpalette())
# and save it out
im2.save("new_image.gif")


