def detect_edges_better(image:Image,threshold:float) -> Image: ''' Author: Mohamed Kaddour Student ID: 101140829 ''' ''' Takes in an original image as well as a thershold as a float and returns and an image with the improved edge detection filter applied based on the value of the threshold. This filter compares the brightness of a pixel's neighbouring pixels and then prints on a filter similar to that of a pencil drawing. >>>show(detect_edges_better(filename,15)) [Displays proper image] ''' new_image = copy(image) width = get_width(new_image) height = get_height(new_image) for x in range(width-1): for y in range(height-1): r1,g1,b1 = get_color(new_image,x,y) r2,g2,b2 = get_color(new_image,x+1,y) r3,g3,b3 = get_color(new_image,x,y+1) brightness1 = (r1+g1+b1)/3 brightnessv = (r2+g2+b2)/3 brightnessr = (r3+g3+b3)/3 if abs(brightness1-brightnessv) >= threshold or abs(brightness1-brightnessr) >= threshold: black = create_color(0,0,0) set_color(new_image, x, y, black) else: white = create_color(255,255,255) set_color(new_image, x, y, white) return new_image
def sepia(original_image: Image) -> Image: """ Developed by Karandev Andotra, 101141882 Reviewed by Hussein Rashid, 101141962 Return a sepia tinted copy of an image from a user-selected image. >>> original_image = load_image(choose_file()) >>> sepia_image = sepia(new_image) """ new_image = grayscale(original_image) for pixel in new_image: x, y, (r, g, b) = pixel brightness = (r + g + b) // 3 if brightness < 63: #dark gray new_color = create_color(1.1 * r, g, 0.9 * b) elif brightness <= 191: #medium gray new_color = create_color(1.15 * r, g, 0.85 * b) elif brightness > 191: #light gray new_color = create_color(1.08 * r, g, 0.93 * b) set_color(new_image, x, y, new_color) return new_image
def test_green_channel() -> None: '''Function Name: Test Green Channel Author: Kiyara Desilva 101165279 ''' '''Takes in an image and checks each pixel to ensure it is green. Prints out pixels being tested, RGB values and "pass" or "fail" >>test_green_channel(green_image) Testing Pixels: 310 402 RGB: 0 6 0 Passed ........ ... ''' original = create_image(6, 1) set_color(original, 0, 0, create_color(12, 44, 122)) set_color(original, 1, 0, create_color(0, 128, 128)) set_color(original, 2, 0, create_color(127, 127, 127)) set_color(original, 3, 0, create_color(125, 73, 224)) set_color(original, 4, 0, create_color(254, 255, 255)) set_color(original, 5, 0, create_color(126, 127, 128)) image = green_channel(original) print("Testing Green Image...") for pixel in image: x, y, (r, g, b) = pixel print("Testing Pixels:", x, y, "RGB:", r, g, b) if (r == 0 and b == 0): print("Passed") else: print("Failed")
def test_invert() -> None: '''A test function for invert. >>> test_invert() ''' # Create an image with three pixels. For testing the invert filter, I picked # (0, 0, 0) and (255, 255, 255) as two of the colours, because they are the # brightest and darkest colours (boundary cases). I picked (128, 127, 128) # as the third colour because it's a non-gray colour in the "middle" of the # set of RGB colour codes. original = create_image(3, 1) set_color(original, 0, 0, create_color(0, 0, 0)) set_color(original, 1, 0, create_color(128, 127, 128)) set_color(original, 2, 0, create_color(255, 255, 255)) # Create an image that's identical to the one a correct implementation of # invert should produce when it is passed original. expected = create_image(3, 1) set_color(expected, 0, 0, create_color(255, 255, 255)) set_color(expected, 1, 0, create_color(127, 128, 127)) set_color(expected, 2, 0, create_color(0, 0, 0)) # Now compare the transformed image returned by the filter with the # expected image, one pixel at a time. inverted = invert(original) for x, y, col in inverted: # 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))
def test_posterize() -> None: '''Function Name: Test Posterize Author: Kiyara Desilva 101165279 ''' ''' Tests the posterize filter function by using a sample image and comparing it with the expected image. >>>test_posterize Checking pixel @(0, 0) PASSED ------ Checking pixel @(1, 0) PASSED ------ Checking pixel @(2, 0) PASSED ''' original = create_image(3, 1) set_color(original, 0, 0, create_color(177, 177, 177)) set_color(original, 1, 0, create_color(0, 0, 0)) set_color(original, 2, 0, create_color(58, 223, 144)) expected = create_image(3, 1) set_color(expected, 0, 0, create_color(159, 159, 159)) set_color(expected, 1, 0, create_color(31, 31, 31)) set_color(expected, 2, 0, create_color(31, 223, 159)) pimage = posterize(original) for x, y, col in pimage: check_equal('Checking pixel @(' + str(x) + ', ' + str(y) + ')', col, get_color(expected, x, y))
def test_red_channel() -> None: '''Function Name: Test Red Channel Author: Adam Burger 101141832 ''' '''Takes in an image and checks each pixel to ensure it is red. Prints out pixels being tested, RGB values and "pass" or "fail" >>>test_red_channel(red_image) Testing Pixels: 541 445 RGB: 104 0 0 Passed ....... ... ''' original = create_image(6, 1) set_color(original, 0, 0, create_color(12, 44, 122)) set_color(original, 1, 0, create_color(0, 128, 128)) set_color(original, 2, 0, create_color(127, 127, 127)) set_color(original, 3, 0, create_color(125, 73, 224)) set_color(original, 4, 0, create_color(254, 255, 255)) set_color(original, 5, 0, create_color(126, 127, 128)) image = red_channel(original) print("Testing Red Image...") for pixel in image: x, y, (r, g, b) = pixel print("Testing Pixels:", x, y, "RGB:", r, g, b) if (g == 0 and b == 0): print("Passed") else: print("Failed")
def test_blue_channel() -> None: """Function tests the blue_channel filter. -Function written by Nathan Gomes - 101143780 >>> blue_channel_test() (0, 0) PASSED ------ (1, 0) PASSED ------ (2, 0) PASSED ------ >>> blue_channel_test() (0, 0) PASSED ------ (1, 0) FAILED: expected Color(red=0, green=0, blue=202), got Color(red=0, green=0, blue=201) ------ (2, 0) PASSED ------ """ original_image = create_image(3, 1) set_color(original_image, 0, 0, create_color(255, 255, 255)) set_color(original_image, 1, 0, create_color(78, 146, 201)) set_color(original_image, 2, 0, create_color(167, 64, 29)) expected_image = create_image(3, 1) set_color(expected_image, 0, 0, create_color(0, 0, 255)) set_color(expected_image, 1, 0, create_color(0, 0, 201)) set_color(expected_image, 2, 0, create_color(0, 0, 29)) blue_image = blue_channel(original_image) for x, y, col in blue_image: check_equal("(" + str(x) + ", " + str(y) + ")", col, get_color(expected_image, x, y))
def test_blue_channel() -> None: '''Function Name: Blue Channel Test Author: Taekwan Oh 101148069 ''' '''Takes in an image and checks each pixel to ensure it is blue. Prints out pixels being tested, RGB values and "pass" or "fail" Testing Pixels: 253 463 RGB: 0 0 88 Passed ........ ... ''' original = create_image(6, 1) set_color(original, 0, 0, create_color(12, 44, 122)) set_color(original, 1, 0, create_color(0, 128, 128)) set_color(original, 2, 0, create_color(127, 127, 127)) set_color(original, 3, 0, create_color(125, 73, 224)) set_color(original, 4, 0, create_color(254, 255, 255)) set_color(original, 5, 0, create_color(126, 127, 128)) image = blue_channel(original) print("Testing Green Image...") for pixel in image: x, y, (r, g, b) = pixel print("Testing Pixels:", x, y, "RGB:", r, g, b) if (r == 0 and g == 0): print("Passed") else: print("Failed")
def two_tone(image: Image, colour1: str, colour2: str) -> Image: """Author: Benjamin Richards Type Annotations: Cimpl.Image, str, str -> Cimpl.Image Takes an image and 2 colours and returns a copy of the image which is made up of only those two colours. If the pixel's brightness is between 0 and 127 it uses the first colour and if it's more than that it uses the second color >>> new = two_tone (original_image, 'blue', 'red') >>> show(new) (shows the image only made up of blue and red).""" #Constants COLOUR_LIST = ["black","white","red","lime","blue","yellow","cyan","magenta" ,"gray"] COLOUR_CODES = [(0,0,0),(255,255,255),(255,0,0),(0, 255, 0),(0, 0, 255), (255, 255, 0),(0, 255, 255),(255, 0, 255),(128, 128, 128)] BRIGHTNESS_LIMIT = 128 # Below this value colour1 is applied and above this # Value colour2 is applied # Replacing the colour texts by the respective colour codes for i in range(len(COLOUR_LIST)): if colour1 == COLOUR_LIST[i]: colour1 = COLOUR_CODES[i] if colour2 == COLOUR_LIST[i]: colour2 = COLOUR_CODES[i] altered_image = copy(image) for x,y, (r,g,b) in image: brightness = (r+g+b)/3 # Checks the brighness value with the limit to decide which color to use if brightness >= BRIGHTNESS_LIMIT: new_colour = create_color(colour2[0], colour2[1], colour2[2]) else: new_colour = create_color(colour1[0], colour1[1], colour1[2]) set_color(altered_image, x, y, new_colour) return altered_image
def red_channel(original_image: Image) -> Image: """Returns the red channel of image without having modified it. - Function written by Malak Abdou - 101139692 >>> red_channel(original_image) <Cimpl.Image object at 0x000001C447BDFAC8> """ red_filter = copy(original_image) for pixel in original_image: x, y, (r, g, b) = pixel red = create_color(r, 0, 0) set_color(red_filter, x, y, red) return red_filter
def red_channel(image: Image) -> Image: """Returns a copy of an image with only redscale pixels. *written by Sam Hurd* >>>red_channel(image) image with all pixels red """ new_image = copy(image) for x, y, (r, g, b) in new_image: red = create_color(r, 0, 0) set_color(new_image, x, y, red) return new_image
def red_channel(image: Image) -> Image: """Return a red filtered copy of image; that is, an image that is showing only the red color of the original image. >>> image = load_image(choose_file()) >>> inverted = invert(image) >>> show(inverted) """ new_image = copy(image) # filter the intensities of every component in every pixel. for x, y, (r, g, b) in image: red = create_color(r, 0, 0) set_color(new_image, x, y, red) return new_image
def detect_edges(image:Image,threshold:float) -> Image: ''' Function Name: Detect Edges Author: Adam Burger 101141832 ''' ''' Takes in an original image as well as a thershold as a float and returns and an image with the edge detection filter applied based on the value of the threshold. This filter compares the brightness of a pixel's lower pixels and then prints on a filter similar to that of a pencil drawing. >>>show(detect_edges_better(filename,15)) [Displays proper image] ''' new_image = copy(image) width = get_width(new_image) height = get_height(new_image) for x in range(width-1): for y in range(height-1): r1,g1,b1 = get_color(new_image,x,y) r2,g2,b2 = get_color(new_image,x,y+1) brightness1 = (r1+g1+b1)/3 brightnessv = (r2+g2+b2)/3 if abs(brightness1-brightnessv) >= threshold: black = create_color(0,0,0) set_color(new_image, x, y, black) else: white = create_color(255,255,255) set_color(new_image, x, y, white) return new_image
def grayscale_from_blue(image: Image) -> Image: """Return a grayscale copy of image. Each pixel's blue component provides the RGB components for the corresponding gray shade. >>> image = load_image(choose_file()) >>> gray_image = grayscale_from_blue(image) >>> show(gray_image) """ new_image = copy(image) for x, y, (r, g, b) in image: gray = create_color(b, b, b) set_color(new_image, x, y, gray) return new_image
def blue_channel(inpic) -> Image: """Returns a copy of the inputed image with only bluescale pixels. written by Yahya Shah >>>blue_channel(image) image with all pixels blue """ pic = copy(inpic) for i, m, (r, g, b) in pic: blue = create_color(0, 0, b) set_color(pic, i, m, blue) return pic
def flip_horizontal(original_image: Image) -> Image: """ Author: Siddharth Natamai - 1011403016 Date: Nov 19, 2019 Returns a original_image after flipping along the x-axis (horizontal flip) >>> flip_horizontal(original_image) <Cimpl.original_image object at 0x7f7ba88dbd10> """ new_image = copy(original_image) img_width = get_width(new_image) img_height = get_height(new_image) img_center = img_height // 2 for x in range(img_width): for y in range(img_center): r, g, b = get_color(new_image, x, y) r2, g2, b2 = get_color(new_image, x, img_height - y - 1) set_color(new_image, x, y, create_color(r2, g2, b2)) set_color(new_image, x, img_height - y - 1, create_color(r, g, b)) return new_image
def blue_channel(original_image: Image) -> Image: """ Developed by Hussein Rashid, 101141962 Return a filtered image from a user-selected original image. RBG code (0,0,b) where b is the original blue pixel value. >>> blue_channel(original_image) """ new_image = copy(original_image) for pixel in original_image: x, y, (r, g, b) = pixel new_color = create_color(0, 0, b) set_color(new_image, x, y, new_color) return new_image
def red_channel(image:Image) -> Image: '''Return a copy of the provided image that is completley red, (r,0,0). Each pixel of the image must be red >>> image = load_image(choose_file()) >>> red_image = red_channel(image) >>> show(red_image) ''' new_image = copy(image) for pixel in image: x, y, (r, g, b) = pixel red = create_color(r,0,0) set_color(new_image, x, y, red) return new_image
def green_channel(original_image: Image) -> Image: """ Developed by Karandev Andotra, 101141882 Return a filtered image from a user-selected original image. RBG code (0,g,0) where g is the original green pixel value. >>> green_channel(original_image) """ new_image = copy(original_image) for pixel in original_image: x, y, (r, g, b) = pixel new_color = create_color(0, g, 0) set_color(new_image, x, y, new_color) return new_image
def flip_vertical(image: Image) -> Image: """Function author : Nathan Gomes - 101143780 Function takes an image and returns a copy of the image that has been flipped vertically (across the "y" axis in an x-y co-ordinate system). >>> original_image = load_image("filename") >>> flip_vertical(original_image) <Cimpl.Image object at 0x000001A90D7A7408> """ new_image = copy(image) mid_pixel = get_width(new_image) // 2 width = get_width(new_image) height = get_height(new_image) for x in range(mid_pixel): for y in range(height): r, g, b = get_color(image, x, y) new_r, new_g, new_b = get_color(image, abs(width - x) - 1, y) set_color(new_image, x, y, create_color(new_r, new_g, new_b)) set_color(new_image, width - x - 1, y, create_color(r, g, b)) return new_image
def blue_channel(image:Image) -> Image: '''Return a copy of the provided image that is completley blue, (0,0,b). Each pixel of the image must be blue. >>> image = load_image(choose_file()) >>> blue_image = blue_channel(image) >>> show(blue_image) ''' new_image = copy(image) for pixel in image: x, y, (r, g, b) = pixel green = create_color(0,0,b) set_color(new_image, x, y, green) return new_image
def green_channel(image:Image) -> Image: '''Return a copy of the provided image that is completley green, (0,g,0). Each pixel of the image must be green >>> image = load_image(choose_file()) >>> green_image = green_channel(image) >>> show(green_image) ''' new_image = copy(image) for pixel in image: x, y, (r, g, b) = pixel green = create_color(0,g,0) set_color(new_image, x, y, green) return new_image
def red_channel(original_image: Image) -> Image: """ Developed by Ahmed Abdellah, 101163588 Return a filtered image from a user-selected original image. RBG code (r,0,0) where r is the original red pixel value. >>> red_channel(original_image) """ new_image = copy(original_image) for pixel in original_image: x, y, (r, g, b) = pixel new_color = create_color(r, 0, 0) set_color(new_image, x, y, new_color) return new_image
def two_tone(original_image: Image, color1: str, color2: str) -> Image: """ Developed by Hussein Rashid, 101141962 Reviewed by Trong Nguyen, 100848232 Return a two-tone copy of an image from a user-selected original image and two color tone inputs. >>> original_image = load_image(choose_file()) >>> two_tone_image = two_tone(original_image,"yellow","cyan") """ new_image = copy(original_image) color_name = [ "black", "white", "red", "lime", "blue", "yellow", "cyan", "magenta", "gray" ] color_code = [(0, 0, 0), (255, 255, 255), (255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (0, 255, 255), (255, 0, 255), (128, 128, 128)] for i in range(9): if color1 == color_name[i]: color_tone1 = color_code[i] new_color1 = create_color(color_tone1[0], color_tone1[1], color_tone1[2]) elif color2 == color_name[i]: color_tone2 = color_code[i] new_color2 = create_color(color_tone2[0], color_tone2[1], color_tone2[2]) for pixel in original_image: x, y, (r, g, b) = pixel brightness = (r + g + b) // 3 if brightness < 128: set_color(new_image, x, y, new_color1) elif brightness >= 128: set_color(new_image, x, y, new_color2) return new_image
def detect_edges_better(original_image: Image, threshold: int) -> Image: """ Developed by Trong Nguyen, 100848232 Reviewed by Ahmed Abdellah, 101163588 Return an improved edge detection modified copy of an image from a user-selected original image and an input threshold value. >>> original_image = load_image(choose_file()) >>> detect_edges_better_image = detect_edges_better(original_image, 10) """ new_image = copy(original_image) width = get_width(original_image) height = get_height(original_image) for x in range(width): for y in range(height): if not y == height - 1 and not x == width - 1: r, g, b = get_color(new_image, x, y) r_down, g_down, b_down = get_color(new_image, x, y + 1) r_right, g_right, b_right = get_color(new_image, x + 1, y) brightness = (r + g + b) // 3 brightness_down = (r_down + g_down + b_down) // 3 brightness_right = (r_right + g_right + b_right) // 3 if abs(brightness - brightness_down) > threshold \ or abs(brightness - brightness_right) > threshold: new_color = create_color(0, 0, 0) else: new_color = create_color(255, 255, 255) set_color(new_image, x, y, new_color) else: new_color = create_color(255, 255, 255) set_color(new_image, x, y, new_color) new_color = create_color(255, 255, 255) set_color(new_image, x, y, new_color) return new_image
def combine(red_pic: Image, green_pic: Image, blue_pic: Image) -> Image: # Author: Siddharth Natamai - 101143016 """Combines the inputted images and returns the final image >>> combine(image_1, image_2, image_3) <Cimpl.Image object at 0x7fab575d3ad0> """ final_image = copy(red_pic) for pixel in final_image: x, y, (r, g, b) = pixel blue_colour = get_color(blue_pic, x, y) green_colour = get_color(green_pic, x, y) new_colours = create_color(r, green_colour[1], blue_colour[2]) set_color(final_image, x, y, new_colours) return final_image
def invert(image: Image) -> Image: """Return an inverted copy of image; that is, an image that is a colour negative of the original image. >>> image = load_image(choose_file()) >>> inverted = invert(image) >>> show(inverted) """ new_image = copy(image) # Invert the intensities of every component in every pixel. for x, y, (r, g, b) in image: inverted = create_color(255 - r, 255 - g, 255 - b) set_color(new_image, x, y, inverted) return new_image
def test_three_tone(original_image: Image): """ Author: Siddharth Natamai - 101143016 Date: Nov 17, 2019 Returns a bool depending on whether the the function is working properly or not >>> test_three_tone() True >>> test_three_tone() False """ filtered_image = three_tone(original_image, "black", "white", "lime") test_state = True for pixel in filtered_image: x, y, (r, g, b) = pixel color_original = get_color(original_image, x, y) color_filtered = get_color(filtered_image, x, y) color_average = (color_original[0] + color_original[1] + color_original[2]) // 3 colour_1 = create_color(0, 0, 0) colour_2 = create_color(255, 255, 255) colour_3 = create_color(0, 255, 0) test_state = bool( (color_average <= 84 and color_filtered == colour_1) or ((84 < color_average <= 170) and color_filtered == colour_2) or (color_average >= 171 and color_filtered == colour_3)) if test_state: print('Test Passed') else: print('Test Failed') return test_state
def test_two_tone(original_image: Image): """ Author: Siddharth Natamai - 101143016 Date: Nov 17, 2019 Returns a bool depending on whether the the function is working properly or not. >>> test_two_tone() True >>> test_two_tone() False """ filtered_image = tt(original_image, "black", "white") test_state = True for pixel in filtered_image: x, y, (r, g, b) = pixel color_original = get_color(original_image, x, y) color_filtered = get_color(filtered_image, x, y) color_average = (color_original[0] + color_original[1] + color_original[2]) // 3 colour_1 = create_color(0, 0, 0) colour_2 = create_color(255, 255, 255) if color_average <= 127 and color_filtered == colour_1 \ or color_average >= 128 and color_filtered == colour_2: test_state = True else: test_state = False if test_state: print('Test Passed') else: print('Test Failed') return test_state
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 one image which is the a combination of all three >>> combined_image = combine(red_image, green_image, blue_image) >>> show(combined_image) ''' new_image = copy(red_image) for pixel in new_image: x, y, (r,b,g) = pixel r1,g1,b = get_color(green_image,x,y) r1,g,b1 = get_color(blue_image,x,y) original = create_color(r,g1,b1) set_color(new_image, x, y, original) return new_image