# File: imageProcessing.py # # Image processing including grayscale, inverse, sepia, # and blurring (at two levels). # # Author: TODO # # Collaboration statement: TODO # # Inputs: image file from graphics import * ########################################################### ## Independent pixels ## ########################################################### ###### Create the images (calls pixel function) ###### 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 ###### Get the color for a single pixel ###### 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 """ # Look up the RGB values at pixel (x,y) r,g,b = image.getPixel(x,y) # Use the grayscale formula to compute # the new color for this pixel y = int(0.2126*r + 0.7152*g + 0.0722*b) # Return the grayscale RGB color 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 ########################################################### ## Blurring images ## ########################################################### ###### Create the images (calls pixel function) ###### 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 ###### Get the (blurred) color for a single pixel ###### 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 ########################################################### ## main ## ########################################################### def processImage(filename, outputFolder=".", waitForClick=False): """ Processes the file specified by filename, drawing a variety of modified versions of the image (each saved in the given output folder location). filename: the name of the file (a string) outputFolder: the location to save images (a string, defaults to '.') waitForClick: whether to wait for a mouse click before closing the window """ # Load the original image image = Image(Point(0,0), filename) 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) grayscaleImage.save(f"{outputFolder}/grayscale.png") # Create the inverse image inverseImage = createInverseImage(image) inverseImage.move(width, 0) inverseImage.draw(win) inverseImage.save(f"{outputFolder}/inverse.png") # Create the sepia image sepiaImage = createSepiaImage(image) sepiaImage.move(width, height) sepiaImage.draw(win) sepiaImage.save(f"{outputFolder}/sepia.png") # Create the blurry image blurryImage = createBlurryImage(image) blurryImage.move(width*2, 0) blurryImage.draw(win) blurryImage.save(f"{outputFolder}/blurry.png") # Create the blurred image blurrierImage = createBlurrierImage(image) blurrierImage.move(width*2, height) blurrierImage.draw(win) blurrierImage.save(f"{outputFolder}/blurrier.png") # Stick around until the user clicks the window (unless running in # the autograder) -- don't change the next two lines! if waitForClick: win.getMouse() def main(): processImage("images/squash.gif", waitForClick=True) if __name__ == "__main__": main() ############################################################## ## Reflection ## ############################################################## # TODO