def flip_vertical(img: Cimpl.Image) -> Cimpl.Image: """ author: himanshu singh flips the image vertically (Cimpl.Image object) and returns it when called. >>> flip_vertical(Cimpl.load_image("image.jpg")) returns a vertically flipped image """ newimage = Cimpl.copy(img) height1 = Cimpl.get_height(img) # get image height width1 = Cimpl.get_width(img) # get image width for y in range(height1): for x in range(width1): change = height1 - y - 1 # editing height to flip vertically (change) # change implemented below to make changes to the actual image passed # in below Cimpl.set_color(newimage, x, y, Cimpl.get_color(img, x, change)) Cimpl.set_color(newimage, x, change, Cimpl.get_color(img, x, y)) return newimage
def detect_edges(original_image: Cimpl.Image, threshold: float) -> Cimpl.Image: """ The author: Ibrahim Kasim returns a copy of the original image with edge detection filter apllied to it.""" new_image = Cimpl.copy(original_image) total_width = Cimpl.get_width(original_image) total_height = Cimpl.get_height(original_image) blacked = Cimpl.create_color(0, 0, 0) whited = Cimpl.create_color(255, 255, 255) for x, y, pixels in original_image: if x != total_width and y != total_height - 1: pixel_top = Cimpl.get_color(original_image, x, y) pixel_bottom = Cimpl.get_color(original_image, x, y + 1) average_top = (pixel_top[0] + pixel_top[1] + pixel_top[2]) / 3 average_bottom = (pixel_bottom[0] + pixel_bottom[1] + pixel_bottom[2]) / 3 difference = abs(average_bottom - average_top) if x == total_width: Cimpl.set_color(new_image, x, y, whited) elif difference > threshold: Cimpl.set_color(new_image, x, y, blacked) else: Cimpl.set_color(new_image, x, y, whited)
def check_equal(expected: Cimpl.Image, outcome: Cimpl.Image) -> None: """ Author: Zakaria Ismail Checks if PARAMETERS expected and outcome, both Cimpl.Image objects are: 1. Of the same type 2. Have the same pixel at each location - Quantitatively the same Assumes both PARAMETERS have the same dimensions <- should this be taken into account? """ errors = 0 if type(expected) == type(outcome): for x, y, exp_col in expected: out_col = Cimpl.get_color(outcome, x, y) if exp_col != out_col: print("ERROR: Color discrepancy detected at x:{} y:{}\n" "Expected: {}\n" "Outcome: {}".format(x, y, exp_col, out_col)) errors += 1 if errors == 0: print( "SUCCESS: expected and outcome are of the same type and have identical pixels." ) else: print("{} ERRORS detected.".format(errors)) else: print("ERROR: Different types detected.\n" "Expected: Type {}\n" "Outcome: Type {}".format(type(expected), type(outcome)))
def check_equal(expected: Cimpl.Image, outcome: Cimpl.Image) -> None: """ Author: Zakaria Ismail RETURNS nothing. Checks if PARAMETERS expected and outcome, both Cimpl.Image objects are: 1. Of the same type 2. Have the same pixel at each location - Quantitatively the same >>> check_equal(Cimpl.load_image('p2-original.jpg'), Cimpl.load_image('combined_image.png')) -> Test results and error messages are printed. """ errors = 0 if type(expected) == type(outcome): for x, y, exp_col in expected: out_col = Cimpl.get_color(outcome, x, y) if exp_col != out_col: print("ERROR: Color discrepancy detected at x:{} y:{}\n" "Expected: {}\n" "Outcome: {}".format(x, y, exp_col, out_col)) errors += 1 if errors == 0: print( "SUCCESS: expected and outcome are of the same type and have identical pixels." ) else: print("{} ERRORS detected.".format(errors)) else: print("ERROR: Different types detected.\n" "Expected: Type {}\n" "Outcome: Type {}".format(type(expected), type(outcome)))
def detect_edges_better(image: Cimpl.Image, threshold: int) -> Cimpl.Image: """ Author:YANGLONG LIU 101141366 returns a Image with only black and white color depending on the comparsion between contrast and threshold. >>>improved_detect_image = detect_edges_better(raw_image, 13) >>>show(improved_detect_image) which returned image looks like pencil skectches. """ new_image = Cimpl.copy(image) wdt = Cimpl.get_width(new_image) hgt = Cimpl.get_height(new_image) for x in range(wdt): for y in range(hgt): if y < hgt - 1: # make a confirmation for the bottom line is not in this range red, green, blue = Cimpl.get_color(image, x, y) red1, green1, blue1 = Cimpl.get_color( image, x, y + 1) # the pixel below the pixel of raw image one red2, green2, blue2 = Cimpl.get_color( image, x - 1, y) # the right one of the pixel brightness1 = (red + blue + green) / 3 brightness2 = (red1 + blue1 + green1) / 3 brightness3 = (red2 + blue2 + green2) / 3 contrast1 = abs(brightness2 - brightness1) contrast2 = abs(brightness3 - brightness1) if contrast1 > threshold or contrast2 > threshold: # make a comparsion the contrast with threshold red, green, blue = 0, 0, 0 # if the contrast is higher, change color to black. black = Cimpl.create_color(red, green, blue) Cimpl.set_color(new_image, x, y, black) else: red, green, blue = 255, 255, 255 # if the contrast is lower, change color to white. white = Cimpl.create_color(red, green, blue) Cimpl.set_color(new_image, x, y, white) elif y == hgt - 1: # the bottom row of the image which the bottom line is in this range red, green, blue = 255, 255, 255 # change to white simply. white = Cimpl.create_color(red, green, blue) Cimpl.set_color(new_image, x, y, white) return new_image
def flip_horizontal(img: Cimpl.Image) -> Cimpl.Image: """ Author: Zakaria Ismail RETURNS a Cimpl.Image that was flipped, after being PASSED img img is a Cimpl.Image object >>> flip_horizontal(Cimpl.load_image(Cimpl.choose_file())) """ hgt = Cimpl.get_height(img) wth = Cimpl.get_width(img) mid_x = wth // 2 copy = Cimpl.copy(img) for y in range(hgt): for x in range(mid_x): # r1, g1, b1 = Cimpl.get_color(img, x, y) # r2, g2, b2 = Cimpl.get_color(img, x, hgt-y) Cimpl.set_color(copy, x, y, Cimpl.get_color(img, wth - x - 1, y)) Cimpl.set_color(copy, wth - x - 1, y, Cimpl.get_color(img, x, y)) return copy
def combine(r_img: Cimpl.Image, g_img: Cimpl.Image, b_img: Cimpl.Image) -> Cimpl.Image: """ Author: Zakaria Ismail RETURNS an ImageObject where three Cimpl.Image objects are passed. Combines the color channels of the three arguments. >>> Cimpl.show(combine(Cimpl.load_image('red_image.png'), Cimpl.load_image('green_image.png'), Cimpl.load_image('blue_image.png')) -> Returns a Cimpl.Image object """ base = Cimpl.copy(r_img) for x, y, (r, g, b) in base: g_r, g_g, g_b = Cimpl.get_color(g_img, x, y) b_r, b_g, b_b = Cimpl.get_color(b_img, x, y) color = Cimpl.create_color(compute_sum(r, g_r, b_r), compute_sum(g, g_g, b_g), compute_sum(b, g_b, b_b)) Cimpl.set_color(base, x, y, color) Cimpl.save_as(base, 'combined_image.png') return base
def sepia(image): """ Author: YANGLONG LIU return a grayscaled Image object after being passed a Image object. """ hgt = Cimpl.get_height(image) wth = Cimpl.get_width(image) for x in range(wth): for y in range(hgt): red, green, blue = Cimpl.get_color(image, x, y) if red < 63: blue = blue * 0.9 red = red * 1.1 elif 63 <= red and 193 >= red: blue = blue * 0.85 red = red * 1.15 elif red > 191: blue = blue * 0.93 red = red * 1.08 new_color = Cimpl.create_color(red, blue, green) Cimpl.set_color(image, x, y, new_color) return image