import urllib2 png = urllib2.urlopen('http://ipython.rossant.net/squirrel.png') im = imread(png) im.shape from PIL import Image img = Image.fromarray((im*255).astype('uint8')) imshow(im) figure(figsize=(10,6)) cmaps = ['Reds', 'Greens', 'Blues'] for i in xrange(3): subplot(1, 3, i + 1) imshow(im[:,:,i], cmap=get_cmap(cmaps[i])) imshow(array(img.rotate(45.))) from scipy.cluster.vq import * M = im[:,:,0].ravel() # centroids contains four color clusters in the image centroids, _ = kmeans(M, 4) # we associate each pixel to one of the four colors (qnt is in array of integer indices) qnt, _ = vq(M, centroids) # finally we obtain the quantized image through fancy indexing clustered = centroids[reshape(qnt, (300, 300))] cmap = matplotlib.colors.ListedColormap([(0,.2,.3), (.85,.1,.13), (.44,.6,.6), (1.,.9,.65)]) imshow(clustered, cmap=cmap) imsave('squirrelama.png', clustered, cmap=cmap)