from graphics import * import random ########################################################### ## Independent pixel manipulation ## ########################################################### def getGrayscaleColor(image, x, y): """ Converts the pixel to grayscale using the formula Y = 0.2126*r + 0.7152*g + 0.0722*b. returns: the result of color_rgb """ r,g,b = image.getPixel(x,y) y = int(0.2126*r + 0.7152*g + 0.0722*b) return color_rgb(y, y, y) def getInverseColor(image, x, y): """ Converts the pixel to its inverse by taking the complement of each color channel. returns: the result of color_rgb """ # TODO: Part 1 return None # replace with your code def getSepiaColor(image, x, y): """ Converts the pixel to sepia tone using the following formula: newR = 0.393*r + 0.769*g + 0.189*b newG = 0.349*r + 0.686*g + 0.168*b newB = 0.272*r + 0.534*g + 0.131*b Note that if any value is over 255, it must be capped at 255. returns: the result of color_rgb """ # TODO: Part 2 return None # replace with your code def createGrayscaleImage(origImage): """ Creates a copy of the original image, and colors it grayscale. returns: the grayscale image """ # Create the copy image = origImage.clone() w, h = image.getWidth(), image.getHeight() for row in range(h): for col in range(w): # Color the pixel using the grayscale color from the # original image newColor = getGrayscaleColor(origImage, col, row) image.setPixel(col, row, newColor) return image def createInverseImage(origImage): """ Creates a copy of the original image, and inverts its colors. returns: the inverse image """ # TODO: Part 1 return Image(Point(0,0), 1, 1) # replace with your code def createSepiaImage(origImage): """ Creates a copy of the original image, and colors it sepia toned. returns: the sepia image """ # TODO: Part 2 return Image(Point(0,0), 1, 1) # replace with your code ########################################################### ## Blurring images ## ########################################################### def getBlurryColor(image, x, y): """ Chooses a color to blur an image by taking the average across each color channel (R/G/B) of the 3x3 square of pixels centered at (x,y). Note that if (x,y) is on a border, it should just be black. returns: the result of color_rgb """ # TODO: Part 3a return None # replace with your code def getBlurrierColor(image, x, y): """ Chooses a color to blur an image by taking the average across each color channel (R/G/B) of the 5x5 square of pixels centered at (x,y). Note that if (x,y) is on a border or one pixel away, it should just be black. returns: the result of color_rgb """ # TODO: Part 3b return None # replace with your code def createBlurryImage(origImage): """ Creates a copy of the original image, and blurs it. returns: the blurry image """ # TODO: Part 3a return Image(Point(0,0), 1, 1) # replace with your code def createBlurrierImage(origImage): """ Creates a copy of the original image, and blurs it a lot. returns: the blurrier image """ # TODO: Part 3b return Image(Point(0,0), 1, 1) # replace with your code ########################################################### ## main ## ########################################################### def main(): # Load the original image image = Image(Point(0,0), "squash.gif") width = image.getWidth() height = image.getHeight() # Create the window to display both images win = GraphWin("Image Processing", width*3, height*2) # Draw the original image image.move(width/2, height/2) image.draw(win) # Create the grayscale image grayscaleImage = createGrayscaleImage(image) grayscaleImage.move(0, height) grayscaleImage.draw(win) # Create the inverse image inverseImage = createInverseImage(image) inverseImage.move(width, 0) inverseImage.draw(win) # Create the sepia image sepiaImage = createSepiaImage(image) sepiaImage.move(width, height) sepiaImage.draw(win) # Create the blurry image blurryImage = createBlurryImage(image) blurryImage.move(width*2, 0) blurryImage.draw(win) # Create the blurred image blurrierImage = createBlurrierImage(image) blurrierImage.move(width*2, height) blurrierImage.draw(win) # Stick around until the user clicks the window win.getMouse() if __name__ == "__main__": main()