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")
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")
Testing Combined Image... Testing Pixels: 225 473 RGB: (151, 144, 90) with RGB: (151, 144, 90) ... ... ... ''' print("Testing Combined Image...") for pixel in combined_image: x, y, (r, g, b) = pixel comb_pix = r,g,b r1,g,b = get_color(red_image,x,y) r,g1,b = get_color(green_image,x,y) r,g,b1 = get_color(blue_image,x,y) acc_pix = (r1,g1,b1) print ("Testing Pixels:",x,y,"RGB:",comb_pix,"with RGB:", acc_pix) if (acc_pix) != (comb_pix): print("FAILED") else: print("PASSED") r_image = "red_image.png" g_image = "green_image.png" b_image = "blue_image.png" r_image = load_image(r_image) g_image = load_image(g_image) b_image = load_image(b_image) combined_image = combine(r_image, g_image, b_image) show(combined_image) combine_test(r_image, g_image, b_image, combined_image)
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)
from Cimpl import choose_file, load_image, copy, create_color, set_color,\ show, Image, get_color def black_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 r = 30 black = create_color(r, 0, 0) set_color(new_image, x, y, black) return new_image show(black_channel(copy(load_image("Bear.jpg"))))
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()
elif filterinput == 'E' or filterinput == "e": image = detect_edges(image, 10) elif filterinput == 'I' or filterinput == "i": image = detect_edges_better(image, 10) elif filterinput == 'V' or filterinput == "v": image = flip_vertical(image) elif filterinput == 'H' or filterinput == "h": image = flip_horizontal(image) return image file = open("batch_sample.txt", "r") lines = file.readlines() for line in lines: #Goes through each line element in the lines list in the file commands = line.split( " ") #Splits each line into a list based on the space image = copy(load_image( commands[0])) #Define the name of the image(constant) filename = commands[1] #Define the filename (constant) del commands[0:2] for filterinput in commands: #Goes through each command in the commands list image = inputanalysis(filterinput, image) save_as(image, filename) #Saves the image after every command is looked at. file.close()
# Main Code # Assumptions: Cimpl.py, T64_image_filters.py, and simple_Cimpl_filters.py are # In the same folder as this program commands = read_file() # Constants THRESHOLD = 10 COLOUR1 = "yellow" COLOUR2 = "magenta" COLOUR3 = "cyan" # A list of all the possible commands ACCEPTED_INPUTS = ["L", "S", "2", "3", "X", "T", "P", "E", "I", "V", "H", "Q"] # A list of all the filters. FILTERS = [ two_tone, three_tone, extreme_contrast, sepia, posterize, detect_edges, detect_edges_better, flip_vertical, flip_horizontal ] for l in commands: # Loads the original image (first element of the line) original_image = load_image(l[0]) for i in range(2, len(l)): # Goes through the commands and applies them to the image original_image = apply_filter(original_image, l[i]) # Saves the final image (second element of the line) save_as(original_image, l[1])
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
Team identifier: 10 Contributing member(s): Trong Nguyen, 100848232 Ahmed Abdellah, 101163588 Karandev Andotra, 101141882 Hussein Rashid, 101141962 Carleton University ECOR 1051 Module 2 Project - Milestone 3 RELEASE = "April 2, 2020" """ from Cimpl import copy, load_image, save_as from T10_user_interface import execute_command #----------------------------------------------------------------- # Batch User Interface Main Script if __name__ == "__main__": batch_file = open(input("Enter batch filename: ")) for line in batch_file: items = line.strip().split() new_image = copy(load_image(items[0])) for i in range(len(items)-2): new_image = execute_command((items[i+2]), new_image, False) save_as(new_image, str(items[1])) batch_file.close()
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))
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) #Blue Filter
''' #Creates the test image original = create_image(6, 1) set_color(original, 0, 0, create_color(0, 0, 0)) 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)) #Proper Image after extreme contrast is applied actual = create_image(6, 1) set_color(actual, 0, 0, create_color(0, 0, 0)) set_color(actual, 1, 0, create_color(0, 255, 255)) set_color(actual, 2, 0, create_color(0, 0, 0)) set_color(actual, 3, 0, create_color(0, 0, 255)) set_color(actual, 4, 0, create_color(255, 255, 255)) set_color(actual, 5, 0, create_color(0, 0, 255)) twot_image = extreme_contrast(original) for x, y, col in twot_image: check_equal('Checking pixel @(' + str(x) + ', ' + str(y) + ')', col, get_color(actual, x, y)) filename = "p2-original.jpg" filename = copy(load_image(filename)) threep_img = extreme_contrast(filename) show(threep_img) test_extreme_contrast()