''' Purpose: implement reflect.py ''' from PIL import Image def loc( img, spot ) : ''' If image coordinate spot lies on the lefthand side of img, the function returns spot. Otherwise, the function returns the reflection location of spot with respect to the center axis of img ''' def create_reflection( original ) : ''' The function makes use of loc() in returning a new image whose left side is that of original and whose right is a reflection of the left side. ''' ow, oh = original.size # get dimensions of original nw, nh = ow, oh # new image has same dimensions new_image = Image.new( 'RGB', ( nw, nh ) ) # create new image for nx in range( 0, nw ) : # fill in every pixel of new image for ny in range( 0, nh ) : nspot = (nx, ny ) # set spot to be filled in new image ospot = loc( original, nspot ) # determine corresponding spot in original opixel = original.getpixel( ospot ) # get pixel at corresponding spot npixel = opixel # use that pixel for the new image new_image.putpixel( nspot, npixel ) # set spot in new image return new_image # return reflection if ( __name__ == '__main__' ) : import url TEST_URL = "http://www.cs.virginia.edu/~cs1112/images/coloring-book/crayoned-girl-and-dog.png" original = url.get_image( TEST_URL ) result = create_reflection( original ) result.save( "reflect-girl-dog.png" )