''' Module manip: provides a basic pattern for image manipulation and convenience functions for some basic transformations ''' from PIL import Image import locate, mutate, dimensions def manip( original, d, c, l ) : ''' Provide a pattern for image manipulation ''' # get dimensions of the original ow, oh = original.size # set dimensions of the new image nw, nh = d( original ) # 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, nw, nh ) # get the pixel at the ospot opixel = original.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( original ) : ''' Return a new duplicate of original ''' new_image = manip( original, dimensions.same, mutate.same, locate.same ) return new_image def negative( original ) : ''' Return a new color negative of original ''' new_image = manip( original, dimensions.same, mutate.neg, locate.same ) return new_image def blue( original ) : ''' Return a new blueing of original ''' new_image = manip( original, dimensions.same, mutate.blue, locate.same ) return new_image def mono( original ) : ''' Return a new black and white version of original ''' new_image = manip( original, dimensions.same, mutate.bw, locate.same ) return new_image def blue( original ) : ''' Return a new blueing of original ''' new_image = manip( original, dimensions.same, mutate.blue, locate.same ) return new_image def greyscale( original ) : ''' Return a new greyscale version of original ''' new_image = manip( original, dimensions.same, mutate.grey, locate.same ) return new_image def sepia_tone( original ) : ''' Return a new sepia-colored version of original ''' new_image = manip( original, dimensions.same, mutate.sepia, locate.same ) return new_image def reduce_palette( original ) : ''' Return a new palette-8 version of original ''' new_image = manip( original, dimensions.same, mutate.simplify, locate.same ) return new_image def mirroring( original ) : ''' Return a new mirror version of original ''' new_image = manip( original, dimensions.same, mutate.same, locate.mirror ) return new_image def flipping( original ) : ''' Return a new flipping version of original ''' new_image = manip( original, dimensions.same, mutate.same, locate.flip ) return new_image def rotate( original ) : ''' Return a new clockwise rotation version of original ''' new_image = manip( original, dimensions.cw, mutate.same, locate.ccw ) return new_image def zoom_in( original ) : ''' Return a new image whose dimensions are twice that of original ''' new_image = manip( original, dimensions.enlarge, mutate.same, locate.enlarge ) return new_image def zoom_out( original ) : ''' Return a new image whose dimensions are half that of original ''' new_image = manip( original, dimensions.shrink, mutate.same, locate.shrink ) return new_image