ColorQuantize should do what you're suggesting. Here is an example that you can try:
im = LinearGradientImage[{Red, Blue}];
ColorQuantize[im, 4]
where I have specified that 4 colors should be displayed. See the doc page for ColorQuantize for more information.