Question
I need to implement a function where it changes the color of an image from colored to a grayscale or colored to sepia. Below is
I need to implement a function where it changes the color of an image from colored to a grayscale or colored to sepia. Below is the specifications
# Function useful for debugging def display(image): """ Returns False after pretty printing the image pixels, one pixel per line. All plug-in functions must return True or False. This function returns False because it displays information about the image, but does not modify it. You can use this function to look at the pixels of a file and see whether the pixel values are what you expect them to be. This is helpful to analyze a file after you have processed it. Parameter image: The image buffer Precondition: image is a 2d table of RGB objects """ height = len(image) width = len(image[0]) # Find the maximum string size for padding maxsize = 0 for row in image: for pixel in row: text = repr(pixel) if len(text) > maxsize: maxsize = len(text) # Pretty print the pixels print() for pos1 in range(height): row = image[pos1] for pos2 in range(width): pixel = row[pos2] middle = repr(pixel) padding = maxsize-len(middle) prefix = ' ' if pos1 == 0 and pos2 == 0: prefix = '[ [ ' elif pos2 == 0: prefix = ' [ ' suffix = ',' if pos1 == height-1 and pos2 == width-1: suffix = (' '*padding)+' ] ]' elif pos2 == width-1: suffix = (' '*padding)+' ],' print(prefix+middle+suffix) # This function does not modify the image return
# Example function illustrating image manipulation def dered(image): """ Returns True after removing all red values from the given image. All plug-in functions must return True or False. This function returns True because it modifies the image. This function sets the red value to 0 for every pixel in the image. Parameter image: The image buffer Precondition: image is a 2d table of RGB objects """ # Get the image size height = len(image) width = len(image[0]) for row in range(height): for col in range(width): pixel = image[row][col] pixel.red = 0 # This function DOES modify the image return True
# IMPLEMENT THESE FOUR FUNCTIONS def mono(image, sepia=False): """ Returns True after converting the image to monochrome. All plug-in functions must return True or False. This function returns True because it modifies the image. It converts the image to either greyscale or sepia tone, depending on the parameter sepia. If sepia is False, then this function uses greyscale. For each pixel, it computes the overall brightness, defined as 0.3 * red + 0.6 * green + 0.1 * blue. It then sets all three color components of the pixel to that value. The alpha value should remain untouched. If sepia is True, it makes the same computations as before but sets green to 0.6 * brightness and blue to 0.4 * brightness. Parameter image: The image buffer Precondition: image is a 2d table of RGB objects Parameter sepia: Whether to use sepia tone instead of greyscale Precondition: sepia is a bool """ # We recommend enforcing the precondition for sepia # Change this to return True when the function is implemented return False
The hint says that you do not need an if statement for the grayscale but when you convert to sepia Use an if-statement to make sure that you do not break grayscale conversion. Your function should produce normal grayscale when sepia is False and sepia tone when it is True.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started