"""Starter code for assignment 6. AUTHORS: - YOUR NAMES HERE - Titus Klinge""" from graphics import * def display_image(img): """Displays the image in a window. PARAMETERS: img, an Image object RETURNS: win, the GraphWin object the image is displayed in POSTCONDITIONS: - A GraphWin window is constructed that is the same dimensions as the image. - The image is displayed and centered in the window.""" win = GraphWin("Image Viewer", img.getWidth(), img.getHeight()) # Below, we set the coordinates so that img is centered # in the window so that when drawn it takes up the full # space of the window anchor = img.getAnchor() x, y = anchor.getX(), anchor.getY() win.setCoords(x-1, y-1, x+1, y+1) img.draw(win) return win def image_transform(img, effect): """Creates a new image by applying the given effect to every pixel in the given image. PARAMETERS: img, an Image object effect, a function RETURNS: new_img, an Image object PRECONDITIONS: - The "effect" parameter is a function that takes an input of the form [r,g,b] and returns an input of the form [r,g,b] POSTCONDITIONS: - The new_img object returned is an exact copy of img except that each pixel = [r,g,b] in the original img is replaced by the pixel returned by effect(pixel)""" new_img = Image(img.getAnchor(), img.getWidth(), img.getHeight()) # For each pixel in the old image ... for y in range(img.getHeight()): for x in range(img.getWidth()): pixel = img.getPixel(x, y) # Modify the pixel using the given effect and makes sure # that each component is within 0 and 255, inclusive new_pixel = effect(pixel) for i in range(len(new_pixel)): if new_pixel[i] < 0: new_pixel[i] = 0 elif new_pixel[i] > 255: new_pixel[i] = 255 # Converts the pixel into a graphics.py supported format new_color = color_rgb(new_pixel[0], new_pixel[1], new_pixel[2]) # Set the pixel value in the same spot of the new image new_img.setPixel(x, y, new_color) return new_img