Develop three algorithms for lightening, darkening, and color filtering as three related Python functions, lighten, darken, and colorFilter. The first two functions should expect an image and a positive integer as arguments. The third function should expect an image and a tuple of integers (the RGB values) as arguments. The following session shows how these functions can be used with the hypothetical images image1, image2, and image3, which are initially transparent: >>> image1 = Image(100, 50) >>> image2 = Image(100, 50) >>> image3 = Image(100, 50) >>> lighten(image1, 128) # Converts to gray >>> darken(image2, 64) # Converts to dark gray >>> colorFilter(image3, (255, 0, 0))
def lighten(image, n): for x, y, (r, g, b) in image: luminance = (r * 299 + g * 587 + b * 114) // 1000 image.setPixel(x, y, (luminance + n, luminance + n, luminance + n)) def darken(image, n): for x, y, (r, g, b) in image: luminance = (r * 299 + g * 587 + b * 114) // 1000 image.setPixel(x, y, (luminance - n, luminance - n, luminance - n)) def colorFilter(image, n): for x, y, (r, g, b) in image: image.setPixel(x, y, (r + n[0], g + n[1], b + n[2]))