def sepia(image): """ (Cimpl.Image) -> Image Filters out all the red and green values of the image and returns only the blue. >>> image = load_image(choose_file()) >>> sepia(image) >>> show(image) """ new_image = grayscale(image) for pixel in new_image: x, y, (r, g, b) = pixel if (r < 63 and b < 63): darkgray = create_color(r * 1.1, g, b * 0.9) set_color(new_image, x, y, darkgray) elif (63 <= r <= 191 and 63 <= b <= 191): medgray = create_color(r * 1.15, g, b * 0.85) set_color(new_image, x, y, medgray) elif (r > 191 and b > 191): lightgray = create_color(r * 1.08, g, b * 0.93) set_color(new_image, x, y, lightgray) return new_image
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 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 sepia(image: Image) -> Image: #Junayd DeMone #101186381 ''' returns an image with a sepia tint apllied >>>sepia(load_image('p2-original.png')) sepia tinted image ''' new_image = grayscale(image) height = get_height(image) width = get_width(image) new_image2 = copy(new_image) for x in range(width): for y in range(height): c = get_color(new_image2, x, y) r, g, b = c if g < 63: cc = create_color(r * 1.1, g, b * 0.9) set_color(new_image2, x, y, cc) if 63 <= g <= 191: cc = create_color(r * 1.15, g, b * 0.85) set_color(new_image2, x, y, cc) if 191 < g: cc = create_color(r * 1.08, g, b * 0.93) set_color(new_image2, x, y, cc) return new_image2
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 sepia(image: Image) -> Image: """Author: Ian Holmes Type Annotations: Cimpl.Image -> Cimpl.Image Return a new image with the sepia filter applied. The image is first converted to gray using grayscale and then a yellow tint is added (The grayscale is taken from the Cimple library) >>> new = sepia(image) >>> show(new) """ #turning image to grayscale new_image = grayscale(image) #setting limits for which pixels are considered dark, medium, or light. DARK_GRAY_LIMIT = 63 MEDIUM_GRAY_LIMIT = 191 # Red value is multiplied is multiply by one of these factors depending on # The range in which it is located RED_FACTOR_LOW = 1.1 RED_FACTOR_MID = 1.15 RED_FACTOR_HIGH = 1.08 # Blue value is multiplied is multiply by one of these factors depending on # The range the red value is located at BLUE_FACTOR_LOW = 0.9 BLUE_FACTOR_MID = 0.85 BLUE_FACTOR_HIGH = 0.93 #tinting pixels yellow for x,y, (r,g,b) in new_image: # Depending on the red value the red value and the blue are multiplied # By a constant if r < DARK_GRAY_LIMIT: r1,b1 = r * RED_FACTOR_LOW, b * BLUE_FACTOR_LOW elif r >= DARK_GRAY_LIMIT and r <= MEDIUM_GRAY_LIMIT: r1,b1 = r * RED_FACTOR_MID, b * BLUE_FACTOR_MID elif r > MEDIUM_GRAY_LIMIT: r1,b1 = r * RED_FACTOR_HIGH, b * BLUE_FACTOR_HIGH sepia = create_color(r1,g,b1) set_color(new_image, x, y, sepia) return new_image
def test_grayscale() -> None: '''A test function for grayscale. >>> test_grayscale() ''' # This test function checks if grayscale correctly transforms: # (0, 0, 0) to (0, 0, 0) # the darkest gray shade # (0, 0, 1) to (0, 0, 0) # the darkest non-gray shade # (127, 127, 127) to (127, 127, 127) # a mid-range gray shade # (125, 73, 224) to (140, 140, 140) # a non-gray colour # (254, 255, 255) to (254, 254, 254) # the brightest non-gray shade # (255, 255, 255) to (255, 255, 255) # the brightest gray shade # Create an image with six pixels. original = create_image(6, 1) set_color(original, 0, 0, create_color(0, 0, 0)) set_color(original, 1, 0, create_color(0, 0, 1)) 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(255, 255, 255)) # Create an image that's identical to the one a correct implementation of # grayscale should produce when it is passed original. expected = create_image(6, 1) set_color(expected, 0, 0, create_color(0, 0, 0)) set_color(expected, 1, 0, create_color(0, 0, 0)) set_color(expected, 2, 0, create_color(127, 127, 127)) set_color(expected, 3, 0, create_color(140, 140, 140)) set_color(expected, 4, 0, create_color(254, 254, 254)) set_color(expected, 5, 0, create_color(255, 255, 255)) # Now compare the transformed image returned by the filter with the # expected image, one pixel at a time. gray_image = grayscale(original) for x, y, col in gray_image: # 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 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 sepia(image: Image) -> Image: """ Author: Francesca Siconolf (101148917) (Cimpl.Image) -> Cimpl.Image Passes the input file (picture) through a sepia filter, first by setting all pixels to an equal grey tone, then multiplying the red and blue values, depending on the value of them. >>>sepia(original_image) *Displays image with sepia filter* """ copy1 = copy(image) new = grayscale(copy1) for pixel in new: x, y, (r, g, b) = pixel b_new = b r_new = r if r < 63: b_new = b * 0.9 r_new *= 1.1 new_colour = create_color(r_new, g, b_new) elif 63 <= r <= 191: b_new *= 0.85 r_new *= 1.15 new_colour = create_color(r_new, g, b_new) else: b_new *= 0.93 r_new *= 1.08 new_colour = create_color(r_new, g, b_new) set_color(new, x, y, new_colour) return new