''' Module manip: provides a basic pattern for image manipulation and convenience functions for some basic transformations ''' from PIL import Image def same_size( image ) : return image.size def same_color( pixel ) : return pixel def same_at( spot, w, h ) : return spot def manip( original_image, size=same_size, color=same_color, at=same_at ) : ''' Provide a pattern for image manipulation. function size(): determines new image size based on the original function color(): determines pixelation based on the original pixel function at(): determines pixel location relative to original ''' # set dimensions of the new image nw, nh = size( original_image ) # get a new appropriately sized image new_image = Image.new( 'RGB', ( nw, nh ) ) # fill in every pixel of the new image for nx in range( 0, nw ) : # consider every x value for the new image for ny in range( 0, nh ) : # in tandem with every y value for the image # set the spot to be filled in the new image nspot = ( nx, ny ) # determine the corresponding spot of interest in the original ospot = at( nspot, nw, nh ) # get the pixel at the ospot opixel = original_image.getpixel( ospot ) #print(opixel) # determine the pixel for the new image npixel = color( opixel ) # set the nspot in the new image new_image.putpixel( nspot, npixel ) # return the filled in new image return new_image if ( __name__ == '__main__' ) : import url from manip import manip # get web image of interest link = "http://www.cs.virginia.edu/~cs1112/images/mandrill/full.png" original_image = url.get_image( link ) new_image = manip( original_image ) new_image.show()