''' Module manip: provides a basic pattern for image manipulation and convenience functions for some basic transformations ''' from PIL import Image import locate, color, dim def manip( orig, d, c, l ) : ''' Provide a pattern for image manipulation ''' # get dimensions of the original ow, oh = orig.size # set dimensions of the new image nw, nh = d( orig ) # 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 = l( nspot, ow, oh ) # get the pixel at the ospot opixel = orig.getpixel( ospot ) # determine the pixel for the new image npixel = c( opixel ) # set the nspot in the new image new_image.putpixel( nspot, npixel ) # return the filled in new image return new_image def duplicate( orig ) : ''' Return a new duplicate of orig ''' new_image = manip( orig, dim.iso, color.dup, locate.iso ) return new_image def negative( orig ) : ''' Return a new color negative of orig ''' new_image = manip( orig, dim.iso, color.neg, locate.iso ) return new_image def blue( orig ) : ''' Return a new blueing of orig ''' new_image = manip( orig, dim.iso, color.blue, locate.iso ) return new_image def mono( orig ) : ''' Return a new black and white version of orig ''' new_image = manip( orig, dim.iso, color.bw, locate.iso ) return new_image def blue( orig ) : ''' Return a new blueing of orig ''' new_image = manip( orig, dim.iso, color.blue, locate.iso ) return new_image def greyscale( orig ) : ''' Return a new greyscale version of orig ''' new_image = manip( orig, dim.iso, color.grey, locate.iso ) return new_image def sepia_tone( orig ) : ''' Return a new sepia-colored version of orig ''' new_image = manip( orig, dim.iso, color.sepia, locate.iso ) return new_image def reduce_palette( orig ) : ''' Return a new palette-8 version of orig ''' new_image = manip( orig, dim.iso, color.simplify, locate.iso ) return new_image def mirroring( orig ) : ''' Return a new mirror version of orig ''' new_image = manip( orig, dim.iso, color.dup, locate.mirror ) return new_image def flipping( orig ) : ''' Return a new flipping version of orig ''' new_image = manip( orig, dim.iso, color.dup, locate.flip ) return new_image def rotate( orig ) : ''' Return a new clockwise roation version of orig ''' new_image = manip( orig, dim.cw, color.dup, locate.ccw ) return new_image if ( __name__ == '__main__' ) : import url # get web image of interest link = "http://www.cs.virginia.edu/~cs1112/images/mandrill/full.png" orig = url.get_image( link ) new_image = sepia_tone( orig ) new_image.show()