This Matlab program uses the k-means clustering algorithm to create k-colour images (where k is a specified small number such as 4) from an image containing many hundreds (or even thousands) of colours.
To test this out, put all the files in the same folder and run ConvertImage.m. You can use clocktower.jpg to test it out.