# images_lab.py # YOUR NAMES HERE, and Titus Klinge # # A starter file for the image processing lab. from graphics import * # Displays a window showcasing all the colors given in the # list of colors. # # PARAMETERS: # colors, a list of colors # # RETURNS: # win, a GraphWin object # # PRECONDITIONS: # - Each color in colors must be a list of the form [r,g,b] where # r, g, and b are integers between 0 and 255, inclusive # # POSTCONDITIONS: # - A GraphWin window is constructed with a 64x64 pixel square # for each color given in colors. # - The GraphWin object is both displayed and returned as # a value. def color_swatch(colors): height = 64 width = height * len(colors) win = GraphWin("Color Swatch", width, height, autoflush=False) for i in range(len(colors)): cur_color = color_rgb(colors[i][0], colors[i][1], colors[i][2]) for row in range(height): for x in range(height): column = (height * i) + x win.plotPixel(column, row, cur_color) win.flush() return win # 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. def display_image(img): 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 # 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)" def image_transform(img, effect): 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 # Given a color of the form rgb = [r,g,b], returns the same # color but with its red component boosted by 64. # # PARAMETERS: # rgb, a color of the form [r,g,b] # # RETURNS: # new_rgb, a color of the form [r,g,b] # # PRECONDITIONS: # - The parameter is of the form rgb = [r,g,b] and each component # is an integer within the range of 0 and 255 # # POSTCONDITIONS: # - If the parameter rgb is equal to [r,g,b], # then the output is equal to [r+64,g,b] def redder(rgb): red, green, blue = rgb[0], rgb[1], rgb[2] return [red+64, green, blue]