# images_lab.py # YOUR NAMES HERE, and Titus Klinge # # A starter file for the image processing lab. from graphics import * def display_color(r, g, b): """Displays a window showcasing the given rgb color Parameters: r: an integer g: an integer b: an integer Returns: a GraphWin object Preconditions: - r, g, and b are integers between 0 and 255, inclusive Postconditions: - A GraphWin window is created with 128x128 pixels that is filled with the given color """ win = GraphWin("Color Swatch", 128, 128) win.setBackground(color_rgb(r,g,b)) return win def color_swatch(colors): """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. """ height = 64 width = height * len(colors) win = GraphWin("Color Swatch", width, height) pos = 0 for c in colors: cur_color = color_rgb(c[0], c[1], c[2]) cur_rect = Rectangle(Point(pos, 0), Point(pos+height, height)) cur_rect.setFill(cur_color) cur_rect.draw(win) pos = pos + height 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]