import ppm # # constants for accessing color components of pixels # RED = 0 GREEN = 1 BLUE = 2 # # Create a gray scale version of the image provided, returning a new # image. The image passed as a parameter is not modified. # # Parameters: image (ppm format) # # Returns: new image in greyscale # def grayscale(original): # # Create a new image with the same dimensions as the original # gray = ppm.createImage(ppm.width(original), ppm.height(original)) # # Copy each pixel from the original and convert it to gray # # For each pixel in the image for i in range(0, ppm.width(original)): for j in range(0, ppm.height(original)): avg = (original[i][j][RED] + original[i][j][GREEN] + original[i][j][BLUE]) / 3 gray[i][j][RED] = avg gray[i][j][GREEN] = avg gray[i][j][BLUE] = avg return gray # # Create a sepia tone version of the image provided, returning a new # image. The image passed as a parameter is not modified. # # # Parameters: image (ppm format) # # Returns: new image in sepia # def sepia(original): # # Create a new image with the same dimensions as the original # new_image = ppm.createImage(ppm.width(original), ppm.height(original)) # # Copy each pixel from the original and convert it to sepia # # For each pixel in the image for i in range(0, ppm.width(original)): for j in range(0, ppm.height(original)): new_image[i][j][RED] = min(255, original[i][j][RED] * 0.393 + original[i][j][GREEN] * 0.769 + original[i][j][BLUE] * 0.189) new_image[i][j][GREEN] = min(255, original[i][j][RED] * 0.349 + original[i][j][GREEN] * 0.686 + original[i][j][BLUE] * 0.168) new_image[i][j][BLUE] = min(255, original[i][j][RED] * 0.272 + original[i][j][GREEN] * 0.534 + original[i][j][BLUE] * 0.131) return new_image def main(): # # make display window white # print "color 255 255 255" print "clear" dd = ppm.loadPPM("DonaldDuck.ppm") lion = ppm.loadPPM("Lion.ppm") # # display Donald Duck in upper left corner, lion underneath # ppm.displayImage(0,0,dd) ppm.displayImage(0,ppm.height(dd),lion) # # Create the gray scale version of donald duck, display beside original # gray = grayscale(dd) ppm.displayImage(ppm.width(dd), 0, gray) # display beside original # # Create a sepia tone version of the lion, display beside original # sep = sepia(lion) ppm.displayImage(ppm.width(sep), ppm.height(dd), sep) # beside original main()