def poor_test_grayscale1() -> None: '''A poorly designed test function for grayscale. This test function has at least two problems: (1) It verifies that each pixel's colour is a shade of gray, but it doesn't determine if it's the CORRECT shade of gray. For example, if grayscale() returns an image in which all pixels were set to (128, 128, 128), regardless of the pixels' colours in the original image, the test would still pass because the condition: r != g or g != b would always be False. (2) The pixels in the original image are "random": they are competely dependant on where the camera was pointing when the photo was taken. Suppose our grayscale filter has a bug, and doesn't correctly process any pixels that are already a shade of gray. If the original image doesn't have any gray pixels, the test function won't uncover this bug. >>> poor_test_grayscale1() ''' image = load_image(choose_file()) gray_image = grayscale(image) for x, y, (r, g, b) in gray_image: if r != g or g != b: print('FAIL: pixel @ ', (x, y), 'is', (r, g, b))
def load_img() -> Tuple[bool, bool, Image]: """ Prompts the user to input an image. Returns the dont_quit variable as True, image_loaded as True, and the image chosen. """ image = load_image(choose_file()) show(image) return True, True, image
def load() -> Image: """ __author__ = "Trong Nguyen" Return a copy of a new image from a user-selected loaded image file. >>> load() """ filename = choose_file() original_image = load_image(filename) new_image = copy(original_image) return new_image
def poor_test_grayscale2() -> None: '''Another poorly designed test function for grayscale. This test function has at least two problems: (1) It attempts to fix problem (1) in the previous poorly designed test function. It gets the actual RGB values from a pixel in the image returned by grayscale, and compares them to the expected RGB values for that pixel; that is, the RGB values that a correct implementation of the filter should calculate. The problem is, what would happen if filter and the test function have the same bug in the code that calculates the RGB values. Suppose both functions calculate each pixel's brightness this way: brightness = r + g + b // 3 instead of using the correct expression: brightness = (r + g + b) // 3 The test will pass, because the (incorrect) actual RGB values match the (incorrect) expected values calculated by the test function. (2) Problem (2) is unchanged from the previous test function: we're testing random pixel colours. >>> poor_test_grayscale2() ''' image = load_image(choose_file()) gray_image = grayscale(image) for x, y, (r1, g1, b1) in gray_image: # r1, g1 and b1 are the ACTUAL RGB values of the pixel @ (x, y) in the # image returned by grayscale. # Calculate the EXPECTED RGB values for the pixel @ (x, y) in the # original image. r2, g2, b2 = get_color(image, x, y) brightness = (r2 + g2 + b2) // 3 # Compare the actual and expected RGB values. if r1 != brightness or g1 != brightness or b1 != brightness: print('FAIL: pixel @ ', (x, y), 'is', (r1, g1, b1), 'not', (brightness, brightness, brightness))
def green_channel() -> Image: """Returns the green chanel of the initial image without having modified it. -Function written by Leanne Matamoros - 101147405 >>> green_channel(original_image) <Cimpl.Image object at 0x0000028A06CD6D88> """ file = choose_file() original_image = load_image(file) green_filter = copy(original_image) for pixel in original_image: x, y, (r, g, b) = pixel green_coloration = create_color(0, g, 0) set_color(green_filter, x, y, green_coloration) return green_filter
def open_image() -> Image: """ Author: Ian Holmes Type annotation: (None) -> Cimple.Image This functions asks prompts the user to select an image that they want to apply the filter to. >>> open_image() (asks the user to select their image) (shows the image to the user) """ FILENAME = choose_file() if FILENAME != "": original_image = load_image(FILENAME) show(original_image) return original_image else: print("No image loaded")
# Becomes WIDTH - x -1 col1 = get_color(new_image, x, y) col2 = get_color(new_image, width-x-1, y) set_color(new_image, x, y, col2) set_color(new_image, width-x-1,y, col1) return new_image #Main Code # Assumption: There is a image stored in the same folder as this script # with the given name if __name__ == "__main__": #Loading images FILENAME_ORIGINAL = choose_file() original_image = load_image(FILENAME_ORIGINAL) print("Showing the original image") show(original_image) #Red Filter print("Applying the red filter") red_image=red_channel (original_image) show(red_image) #Green Filter print("Applying the green filter") green_image=green_channel (original_image) show(green_image)
width = get_width(new_image_vertical) height = get_height(new_image_vertical) for w in range(width): for h in range(height//2): color1 = get_color(new_image_vertical, w, h) color2 = get_color(new_image_vertical, w, height -h-1) set_color(new_image_vertical, w, height-h-1, color1) set_color(new_image_vertical, w, h, color2) return new_image_vertical filename = load_image(choose_file()) show(filename) #MIlESTONE 1 redimage = red_channel(filename) show(redimage) greenimage = green_channel(filename) show(greenimage) blueimage = blue_channel(filename) show(blueimage) show(combine(redimage,greenimage,blueimage)) #MILESTONE 2 show(two_tone(filename, "black", "white")) show(three_tone(filename, "black", "white","cyan")) show(extreme_contrast(filename))
from Cimpl import choose_file, load_image, copy, create_color, set_color,\ show, Image, get_color, create_image, get_width, get_height,\ save_as run = True image = None while run == True: filterinput = input( "L)oad image S)ave-as\n2)-tone 3)-tone X)treme contrast" + " T)int sepia P)osterize\nE)edge detect I)mproved edge detect" + " V)ertical flip H)orizontal flip\nQ)uit\n\n: ") if filterinput == "L" or filterinput == "l": image = copy(load_image(choose_file())) show(image) elif filterinput == "S" or filterinput == "s": if image is None: print("Please load an image") else: filename = input("Please enter a filename with the type of file" + "(example: mypicture.jpg)\n: ") save_as(image, filename) elif filterinput == '2': if image is None: print("Please load an image") else: image = two_tone(image, "yellow", "cyan")
from Cimpl import copy, set_color, create_color, Image, show, choose_file, load_image file = choose_file() original_image = load_image(file) def posterize(original_image: Image) -> Image: """ Author: Siddharth Natamai - 1011403016 Date: Nov 17, 2019 Returns a image after applying a posterizing filter based on values from the _adjust_component function >>> posterize(original_image) <Cimpl.Image object at 0x7f7ba88dbd10> """ new_image = copy(original_image) for pixel in original_image: x, y, (r, g, b) = pixel set_color( new_image, x, y, create_color(_adjust_component(r), _adjust_component(g), _adjust_component(b))) return new_image def _adjust_component(original_val: int) -> int: """Determines where each pixel lies in the 4 quadrants (0 to 63, 64 to 127, 128 to 191, and 192 to 255) and sets the new pixel values to the midpoint of that specific quadrant. >>> _adjust_component(50) 31 >>> _adjust_component(90)
if (g==0 and b==0): passed = True else: #print("FAIL") pix = x,y failed_pixels.append(pix) if len(failed_pixels) > 0: print("Test Failed...Failed Pixels:") print(failed_pixels) else: print("Test Passed") image = load_image(choose_file()) red_image = red_channel(image) show(red_image) test_red_channel(red_image) image = load_image(choose_file()) green_image = green_channel(image) show(green_image) image = load_image(choose_file()) blue_image = blue_channel(image) show(blue_image) def combine(red_image:Image, green_image:Image, blue_image:Image) -> Image: '''Takes in three images, a red one, a green one and a blue one and returns
set_color(blue_img, 5, 0, create_color(0, 0, 255)) # Create an image that's identical to the one a correct implementation of # combine should produce when it is passed the red_img, green_img and blue_img as inputs. expected = create_image(6, 1) set_color(expected, 0, 0, create_color(0, 0, 0)) set_color(expected, 1, 0, create_color(13, 0, 1)) set_color(expected, 2, 0, create_color(255, 127, 127)) set_color(expected, 3, 0, create_color(125, 73, 224)) set_color(expected, 4, 0, create_color(254, 255, 255)) set_color(expected, 5, 0, create_color(87, 13, 255)) # Now compare the transformed image returned by the filter with the # expected image, one pixel at a time. combined_img = combine(red_img, green_img, blue_img) for x, y, col in combined_img: # col is the Color object for the pixel @ (x,y) # There's no need to unpack that object into # RGB components. check_equal('Checking pixel @(' + str(x) + ', ' + str(y) + ')', col, get_color(expected, x, y)) if __name__ == "__main__": red_img = load_image(choose_file()) green_img = load_image(choose_file()) blue_img = load_image(choose_file()) # combine images combined = combine(red_img, green_img, blue_img) show(combined) test_combine()