def combine (image1: Image, image2: Image, image3: Image) -> Image: """ Author: Bardia Parmoun Type Annotations: Cimpl.Image, Cimpl.Image, Cimpl.Image -> Cimpl.Image This functions three filtered images (red, green, blue) and creates a new image by combining the rgb's of these three images. Note that in order for this function to work properly the three input images must be in .png format and should be properly filtered (for example the red image must only have a red component in its rgb). >>> combined_image= combine (red_image,green_image, blue_image) >>> show (combined_image) (This will display the combined image made from the three input images) """ new_image = copy(image1) width = get_width(image1) height = get_height(image1) for x in range(width): for y in range(height): r1,g1,b1= get_color(image1, x, y) r2,g2,b2= get_color(image2, x, y) r3,g3,b3= get_color(image3, x, y) # Combining the there pictures by adding their rgb values new_colour = create_color(r1+r2+r3, g1+g2+g3, b1+b2+b3) set_color (new_image, x,y, new_colour) return new_image
def extreme_contrast(original_image: Image) -> Image: """ Developed by Ahmed Abdellah, 101163588 Reviewed by Karandev Andotra, 101141882 Return an extreme contrast filtered image from a user-selected original image. >>> original_image = load_image(choose_file()) >>> extreme_contrast_image = extreme_contrast(original_image) """ new_image = copy(original_image) for pixel in original_image: x, y, (r, g, b) = pixel if r <= 127: r = 0 elif r >= 128: r = 255 if g <= 127: g = 0 elif g >= 128: g = 255 if b <= 127: b = 0 elif b >= 128: b = 255 new_color = create_color(r, g, b) set_color(new_image, x, y, new_color) return new_image
def three_tone(image: Image, colour1: str, colour2: str, colour3: str) -> Image: '''Returns a copy of an inputted image with three different tones based on user selection. written by: Anita Ntomchukwu >>> three_tone(image, 'gray', 'black', 'lime') image with only gray, black and lime pixels ''' three_tone_image = copy(image) for i in range(len(COLOURS)): if colour1 == COLOURS[i]: tone1 = RGB_VALUES[i] new_c1 = create_color(tone1[0], tone1[1], tone1[2]) if colour2 == COLOURS[i]: tone2 = RGB_VALUES[i] new_c2 = create_color(tone2[0], tone2[1], tone2[2]) if colour3 == COLOURS[i]: tone3 = RGB_VALUES[i] new_c3 = create_color(tone3[0], tone3[1], tone3[2]) for x, y, (r, g, b) in three_tone_image: brightness = (r + g + b) // 3 if 0 <= brightness <= 84: set_color(three_tone_image, x, y, new_c1) elif 85 <= brightness <= 170: set_color(three_tone_image, x, y, new_c2) elif 171 <= brightness <= 255: set_color(three_tone_image, x, y, new_c3) return three_tone_image
def sepia(original:Image) -> Image: """ Function Name: Sepia Author: Kiyara De Silva 101165279 """ """ Returns a greyscale image where the red and blue components are adjusted so the image is slightly yellowish >>>show(sepia(filename)) displays expected image """ gray_image = grayscale(original) new_image = copy(gray_image) for pixel in gray_image: x, y, (r, g, b) = pixel if r < 63: new_colour_filter = create_color(r *1.1, g , b *0.9) set_color (new_image, x, y, new_colour_filter) elif 63 <= r <= 191: new_colour_filter = create_color(r *1.15, g , b *0.85) set_color (new_image, x, y, new_colour_filter) else: new_colour_filter = create_color(r *1.08, g , b *0.93) set_color (new_image, x, y, new_colour_filter) return new_image
def detect_edges(original_image: Image, threshold: int) -> Image: """ Developed by Karandev Andotra, 101141882 Reviewed by Hussein Rashid, 101141962 Return an 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_image = detect_edges(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: r, g, b = get_color(new_image, x, y) r_down, g_down, b_down = get_color(new_image, x, y + 1) brightness = (r + g + b) // 3 brightness_down = (r_down + g_down + b_down) // 3 if abs(brightness - brightness_down) > 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) return new_image
def sepia(original_image: Image) -> Image: """Returns a black and white image which has been tinted yellow. >>> original_image = load_image(choose_file()) >>> sepia(original_image) <Cimpl.Image object at 0x00000212C566DD88> """ new_image = copy(original_image) sepia_filter = grayscale(new_image) for pixel in sepia_filter: x, y, (r, g, b) = pixel if r < 63: r *= 1.1 b *= 0.9 if 63 <= r <= 191: r *= 1.15 b *= 0.85 if r > 191: r *= 1.08 b *= 0.93 new_color = create_color(r, g, b) set_color(sepia_filter, x, y, new_color) return sepia_filter
def sepia(image: Image) -> Image: """Returns a copy of an image with a sepia filter. *Written by Sam Hurd* >>> sepia(image) image with grayscale and a yellow tint """ pic = grayscale(copy(image)) for x, y, (r, g, b) in pic: if r < 63: b = b * 0.9 r = r * 1.1 elif r <= 191: b = b * 0.85 r = r * 1.15 else: b = b * 0.93 r = r * 1.08 color = create_color(r, g, b) set_color(pic, x, y, color) return pic
def detect_edges(image: Image, threshold: float) -> Image: """Returns a copy of an image with the pixels changed to either black or white based on the contrast of the pixel above or below based on inputed threshold. *Written by Sam Hurd* >>>detect_edges(image, 5) """ image = copy(image) height = get_height(image) width = get_width(image) black = create_color(0, 0, 0) white = create_color(255, 255, 255) for x in range(0, width): set_color(image, x, height - 1, white) for y in range(0, height - 1): for x in range(0, width): r, g, b = get_color(image, x, y) brightness1 = (r + g + b) // 3 r2, g2, b2 = get_color(image, x, y + 1) brightness2 = (r2 + g2 + b2) // 3 contrast = abs(brightness1 - brightness2) if contrast >= threshold: set_color(image, x, y, black) else: set_color(image, x, y, white) return image
def extreme_contrast(image:Image) -> Image: ''' Function Name: Extreme Contrast Author: Taekwan Oh 101148069 ''' ''' Returns an extremely contrasted copy of the image passed in by looking at the brightness of each colour in each pixel in the passed in image. >>>show(extreme_contrast(filename)) displays expected image ''' new_image = copy(image) for pixel in image: x, y, (r, g, b) = pixel if 0<=r<=127: r = 0 elif 128<=r<=255: r = 255 if 0<=g<=127: g = 0 elif 128<=g<=255: g = 255 if 0<=b<=127: b = 0 elif 128<=b<=255: b = 255 new_colour = create_color(r,g,b) set_color(new_image, x, y, new_colour) return new_image
def extreme_contrast(original_image: Image) -> Image: """Returns the 'extreme contrast' version of an image without having modified it. - Function written by Malak Abdou - 101139692 >>> original_image = load_image(choose_file()) >>> show(extreme_contrast(original_image)) # Returns a copy of original_image with extreme_contrast filter on it. """ extreme_copy = copy(original_image) for pixel in extreme_copy: x, y, (r, g, b) = pixel if r <= 127: r = 0 else: r = 255 if g <= 127: g = 0 else: g = 255 if b <= 127: b = 0 else: b = 255 extreme_color = create_color(r, g, b) set_color(extreme_copy, x, y, extreme_color) return extreme_copy
def combine(red_image: Image, green_image: Image, blue_image: Image) -> Image: """ Returns a single image that is a combination of the three r, g, b values of the inputed images. written by: Anita Ntomchukwu >>>combine(red_image, green_image, blue_image) image that looks exactly like the image before being passed through the red, green and blue channel filters. """ original_image1 = red_image original_image2 = green_image original_image3 = blue_image comb = copy(original_image1) for x, y, (r, g, b) in comb: red = get_color(original_image1, x, y)[0] green = get_color(original_image2, x, y)[1] blue = get_color(original_image3, x, y)[2] new_color = create_color(red, green, blue) set_color(comb, x, y, new_color) return comb
def two_tone(image: Image, colour1: str, colour2: str) -> Image: '''Returns a copy of inputted image with two different tones based on user selection. written by: Anita Ntomchukwu >>> two_tone(image, 'red', 'white') image with only red and white pixels ''' two_tone_image = copy(image) for i in range(len(COLOURS)): if colour1 == COLOURS[i]: tone1 = RGB_VALUES[i] new_c1 = create_color(tone1[0], tone1[1], tone1[2]) if colour2 == COLOURS[i]: tone2 = RGB_VALUES[i] new_c2 = create_color(tone2[0], tone2[1], tone2[2]) for x, y, (r, g, b) in two_tone_image: brightness = (r + g + b) // 3 if 0 <= brightness <= 127: set_color(two_tone_image, x, y, new_c1) elif 128 <= brightness <= 255: set_color(two_tone_image, x, y, new_c2) return two_tone_image
def flip_horizontal (image: Image) -> Image: """ Author: Bardia Parmoun Type Annotations: Cimpl.Image -> Cimpl.Image This function takes an image, makes a copy of it and then it flips it along the horizontal axis. It does that by swapping the colours of the pixels that are the same distance from the top and bottom of the image. >>> new=flip_horizontal (original_image) >>> show(new) (It will display a version of the image that has been flipped along the horizontal axis) """ new_image = copy(image) WIDTH = new_image.get_width() HEIGHT = new_image.get_height() for x in range (WIDTH): for y in range (HEIGHT//2): # Only half of the height is needed # Replaces the colour of the pixel with a pixel that is equally # Distant from the bottom of the image (Same x value but the y value # Becomes HEIGHT - y -1 col1 = get_color(new_image,x,y) col2 = get_color(new_image,x,HEIGHT-y-1) set_color(new_image, x,y, col2) set_color(new_image, x,HEIGHT-y-1, col1) return new_image
def flip_vertical(image: Image) -> Image: """Author: Ian Holmes Type Annotations: Cimpl.Image -> Cimpl.Image Return a copy of the inputted image, with the pixels flipped along the y axis. >>> new = flip_horizontal(image) >>> show(new) """ new_image = copy(image) width = get_width(image) height = get_height(image) for y in range(height): for x in range(width//2): # Replaces the colour of the pixel with a pixel that is equally # Distant from the right of the image (Same y value but the x value # 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
def detect_edges(original_image: Image, threshold: float) -> Image: """Takes an image, makes a copy of it and changes the color of the pixels to black or white, depending on their contrast. - Function written by Malak Abdou - 101139692 >>> original_image = load_image(choose_file()) >>> show(detect_edges(original_image, 15)) # Returns image with detect_edges filter applied with a threshold of 15 without modifying original. """ original2 = copy(original_image) black = create_color(0, 0, 0) white = create_color(255, 255, 255) for x in range(get_width(original2)): for y in range(get_height(original2) - 1): r, g, b = get_color(original2, x, y) r1, g1, b1 = get_color(original2, x, (y + 1)) brightness = (r + g + b) / 3 brightness1 = (r1 + g1 + b1) / 3 if abs(brightness - brightness1) > threshold: set_color(original2, x, y, black) else: set_color(original2, x, y, white) return original2
def extreme_contrast(inpic: Image) -> Image: """Returns a copy of the inputed image with all pixel values set to their maximum. Author: Yahya Shah >>>extreme_contrast(image) image with only black or white pixels """ pic = copy(inpic) for i, m, (r, g, b) in inpic: if r < 128: r = 0 else: r = 255 if g < 128: g = 0 else: g = 255 if b < 128: b = 0 else: b = 255 newcol = create_color(r, g, b) set_color(pic, i, m, newcol) return pic
def three_tone(image: Image, colour_1: str, colour_2: str, colour_3: str)\ -> Image: """ Function takes an image and three colours as strings from the given list: black white red lime blue yellow cyan magenta gray Function returns an image in three tones as per the colours given in the second, third and fourth function parameters, decided by an individual pixel's brightness. -Function written by Nathan Gomes, 101143780 >>> image_1 = load_image(choose_file()) >>> three_tone_image = three_tone(image_1, "blue", "gray", "white") >>> show(three_tone_image) >>> three_tone(image_1, "yellow", "cyan", "purple") #Error because colour passed ("purple") is not in the given list """ black = create_color(0, 0, 0) white = create_color(255, 255, 255) red = create_color(255, 0, 0) lime = create_color(0, 255, 0) blue = create_color(0, 0, 255) yellow = create_color(255, 255, 0) cyan = create_color(0, 255, 255) magenta = create_color(255, 0, 255) gray = create_color(128, 128, 128) colours = [("black", black), ("white", white), ("red", red), ("lime", lime), ("blue", blue), ("yellow", yellow), ("cyan", cyan), ("magenta", magenta), ("gray", gray)] new_image = copy(image) for pixel in new_image: x, y, (r, g, b) = pixel average = (r + g + b) / 3 if (average >= 0) and (average < 85): for i in range(len(colours)): if colour_1 == colours[i][0]: set_color(new_image, x, y, colours[i][1]) elif (average > 84) and (average < 171): for i in range(len(colours)): if colour_2 == colours[i][0]: set_color(new_image, x, y, colours[i][1]) elif (average > 170) and (average < 256): for i in range(len(colours)): if colour_3 == colours[i][0]: set_color(new_image, x, y, colours[i][1]) 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 new_image: x, y, (r, g, b) = pixel 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 detect_edges_better(original_image: Image, threshold: float) -> Image: """Returns an image that looks like a pencil sketch of the original image, changing the pixels' colors to black or white. - Function written by Leanne Matamoros - 101147405 >>> imp_edge(original_image, 15) <Cimpl.Image object at 0x000002096DEEA148> """ edges_copy = copy(original_image) width = get_width(edges_copy) height = get_height(edges_copy) for x in range(width): for y in range(height): r, g, b = get_color(edges_copy, x, y) brightness = (r + g + b) / 3 if y != (height - 1): r1, g1, b1 = get_color(edges_copy, x, (y + 1)) brightness1 = (r1 + g1 + b1) / 3 if x != (width - 1): r2, g2, b2 = get_color(edges_copy, (x + 1), y) brightness2 = (r2 + g2 + b2) / 3 black = create_color(0, 0, 0) white = create_color(255, 255, 255) if x == (width - 1) and y == (height - 1): set_color(edges_copy, x, y, white) elif x != (width - 1) or y != (height - 1): if abs(brightness - brightness1) > threshold or abs( brightness - brightness2) > threshold: set_color(edges_copy, x, y, black) elif abs(brightness - brightness1) < threshold or abs( brightness - brightness2) < threshold: set_color(edges_copy, x, y, white) elif x == (width - 1): if abs(brightness - brightness1) > threshold: set_color(edges_copy, x, y, black) elif abs(brightness - brightness1) < threshold: set_color(edges_copy, x, y, white) elif y == (height - 1): if abs(brightness - brightness2) > threshold: set_color(edges_copy, x, y, black) elif abs(brightness - brightness2) < threshold: set_color(edges_copy, x, y, white) return edges_copy
def three_tone(image: Image, colour1: str, colour2: str, colour3: str) -> Image: '''' Function Name: three_tone Author: Mohamed Kaddour 101140829 ''' '''Returns a filtered image consisting of the three colours passed in as strings from a list of colors: black white red lime yellow cyan magenta gray >>> three_tone(filename, "red", "blue") -> Displays proper image. >>> three_tone(filename, "green", "purple") -> Displays incorrect image. ''' colours = { 'black': (0, 0, 0), 'white': (255, 255, 255), 'red': (255, 0, 0), 'lime': (0, 255, 0), 'blue': (0, 0, 255), 'yellow': (255, 255, 0), 'cyan': (0, 255, 255), 'magenta': (255, 0, 255), 'gray': (128, 128, 128) } new_image = copy(image) new_colour = create_color(0, 0, 0) rgb = (0, 0, 0) for pixel in image: x, y, (r, g, b) = pixel avg = (r + b + g) / 3 if 0 <= avg <= 84: rgb = colours[colour1] new_colour = create_color(rgb[0], rgb[1], rgb[2]) set_color(new_image, x, y, new_colour) elif 85 <= avg <= 170: rgb = colours[colour2] new_colour = create_color(rgb[0], rgb[1], rgb[2]) set_color(new_image, x, y, new_colour) elif 170 <= avg <= 255: rgb = colours[colour3] new_colour = create_color(rgb[0], rgb[1], rgb[2]) set_color(new_image, x, y, new_colour) return new_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 detect_edges_better(img: Image, threshold: int) -> Image: """ Returns a copy of an image with the pixels changed to either black or white based on the contrast of the pixel above, below, or to the right based on the inputed threshold. Author: Anita Ntomchukwu >>>detect_edges(img, 5) """ new_img = copy(img) height = get_height(img) width = get_width(img) black = create_color(0, 0, 0) white = create_color(255, 255, 255) set_color(new_img, width - 1, height - 1, white) for x in range(0, width): set_color(new_img, x, height - 1, white) for y in range(0, height - 1): for x in range(0, width): color1 = get_color(img, x, y) color2 = get_color(img, x, y + 1) brightness1 = (color1[0] + color1[1] + color1[2]) // 3 brightness2 = (color2[0] + color2[1] + color2[2]) // 3 difference1 = abs(brightness1 - brightness2) if difference1 > threshold: set_color(new_img, x, y, black) else: set_color(new_img, x, y, white) for y in range(0, height): for x in range(0, width - 1): color3 = get_color(img, x, y) color4 = get_color(img, x + 1, y) brightness3 = (color3[0] + color3[1] + color3[2]) // 3 brightness4 = (color4[0] + color4[1] + color4[2]) // 3 difference2 = abs(brightness3 - brightness4) if difference2 > threshold: set_color(new_img, x, y, black) else: set_color(new_img, x, y, white) return new_img
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 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 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: """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 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 detect_edges_better (image:Image, threshold: float) -> Image: """ Author: Benjamin Richards Type Annotations: Cimpl.Image, float -> Cimpl.Image This function tries to find the edges of a given image by going through every pixel and checking its brightness with the one below and to the right of it. That difference is compared with a given threshold value. In other words the function will produce a black and white version of the image where the background is white and the main parts of the image is black. >>> new_image= detect_edge_better(copied_image,10) >>> show(new_image) (Shows the new black and white image generated by detecting edges) """ new_image = copy(image) # Constants WIDTH = new_image.get_width() HEIGHT = new_image.get_height() BACK = (255,255,255) EDGE = (0,0,0) for x in range (WIDTH): for y in range (HEIGHT): r,g,b = get_color(new_image,x,y) brightness = (r+g+b)/3 # The default colour value is set to BACK (white). This value later # Can change to EDGE (black) if the difference exceeds the threshold new_colour = create_color(BACK[0],BACK[1],BACK[2]) if y != HEIGHT-1 and x != WIDTH -1: # Makes sure the range doesn't exceed the height and the width # Calculates the brightness of the pixel below it r_down,g_down,b_down = get_color(new_image,x,y+1) brightness_down = (r_down+g_down+b_down)/3 r_right,g_right,b_right=get_color(new_image,x+1,y) brightness_right = (r_right+g_right+b_right)/3 # Checks to see if the difference between the brightness values # Exceeds the threshold and if so converts it to black if (abs(brightness-brightness_down) > threshold or abs(brightness-brightness_right) > threshold): new_colour= create_color (EDGE[0],EDGE[1],EDGE[2]) set_color(new_image, x, y, new_colour) 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 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