''' Purpose: provide means of reducing the color palette of an image ''' from PIL import Image def shrink( original, p ) : ''' returns a new_image image where colors from the original are replaced with their closest palette correspondents of color palette p ''' ow, oh = original.size nw, nh = ow, oh new_image = Image.new( 'RGB', ( nw, nh ) ) for nx in range( 0, nw ) : for ny in range( 0, nh ) : nspot = ( nx, ny ) ospot = ( nx, ny ) opixel = original.getpixel( ospot ) npixel = closest( opixel, p ) new_image.putpixel( nspot, npixel ) return new_image def closest( pixel, p ) : ''' Returns the color in palette p closest to pixel ''' pass def distance( c1, c2 ) : ''' returns the distance between c1 and c2 ''' pass def generate_random_palette( n ) : # generate a palette p of n random colors import random p = [] for i in range( 0, n ) : pass return p if ( __name__ == '__main__') : import url import palette # define our palettes B = [ (0,0,0) ] BW = B + [ (255,255,255) ] BWRGB = BW + [ (0,255,0), (0,0,255), (255,0,0) ] BWRGBMCY = BWRGB + [ (255,255,0), (255,0,255), (0,255,255) ] rand_palette = palette.generate_random_palette( 12 ) palettes = [ B, BW, BWRGB, BWRGBMCY, rand_palette ] location = input( 'Enter image location: ' ) original = url.get_image( location ) for p in palettes : im = palette.shrink( original, p ) im.show()