Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CodeHs question - Darken filter, thank you! Here is the brighten filter example: In this exercise we'll get some practice modifying pixels with the Image
CodeHs question - Darken filter, thank you!
Here is the brighten filter example:
In this exercise we'll get some practice modifying pixels with the Image class. YOUR JOB Write a darken_filter and change_picture function that modifies the pixels in the left half of the image to be darker. You can darken a pixel by subtracting the same value from each of the red, green, and blue values of the pixel. for every pixel in the left half of the image set pixel red to be (red - DARKENING_FACTOR) set pixel green to be (green - DARKENING_FACTOR) set pixel blue to be (blue - DARKENING_FACTOR) Of course, we cannot have negative values for red, green, or blue, so make sure to limit the pixel values to not go below 0. EXAMPLE: BEFORE: AFTER: ca HINT: This exercise is very similar to the previous example Brighten Filter. The main difference is, instead of adding the same value to each pixel, we are subtracting the same value from each pixel. We are also only applying this on half the picture. Looking at the Brighten Filter example and really understanding that code is a good starting place for writing the Darken Filter. 8.7.10 Darken Filter Submit + 1 # Constants for the image 2. IMAGE URL "https://codehs.com/uploads/e07cd01271cac589CC9ef1bf01206a0c" 3 IMAGE WIDTH 280 4 IMAGE HEIGHT = 200 5 DARKENING FACTOR 50 6 MIN PIXEL VALUE = 0 7 8 image = Image (IMAGE_URL) 9 image.set_position(70, 70) image.set_size(IMAGE_WIDTH, IMAGE_HEIGHT) 11 add(image) 12 13 # This function should take a pixel and return a tuple 14 # with the new color 15- def darken_filter(pixel): 16 pass 17 18 # This function should loop through each pixel on the # left hand side and call the darken filter to calculate 20 # the new color then update the color. 21 - def change_picture(): 22 pass 23 24 # Give the image time to load 25 print("Making Darker....") 26 print("Might take a minute. ...") 27 timer.set_timeout(change_picture, 1000) 28 19 4 6 8.7.6 Brighten Filter 1 # Constants for the image 2 IMAGE_URL = "https://codehs.com/uploads/e07cd01271cac589cc9ef1bf012c6ac" IMAGE_WIDTH = 280 IMAGE_HEIGHT = 200 5 BRIGHTENING_FACTOR 50 MAX_PIXEL_VALUE = 255 7 8 image Image (IMAGE_URL) 9 image.set_position(70, 70) 10 image.set_size(IMAGE_WIDTH, IMAGE_HEIGHT) 11 add (image) 12 13 14 # Filter to brighten an image 15- def brighten_filter(pixel): 16 new_red = brighten_color(pixel[@]) 17 new_green - brighten_color(pixel[1]) 18 new_blue = brighten_color(pixel[2]) 19 return (new_red, new_green, new_blue) 20 def brighten_color(color_value): 22 #If the value is over 255, set it to 255 23 return min(color_value + BRIGHTENING_FACTOR, MAX_PIXEL_VALUE) 24 25- def change_picture(): 26 for x in range(image.get_width()): 27 - for y in range (image.get_height(): pixel = image.get_pixel(x,y) 29 new_colors = brighten_filter(pixel) 30 image.set_red(x, y, new_colors[0]) 31 image.set_green(x, y, new_colors[1]) 32 image.set_blue(x, y, new_colors[2]) 33 34 35 # Give the image time to load 36 print("Making Brighter....") 37 print("Might take a minute....") 38 timer.set_timeout(change_picture, 1000)|| 21 28 NmStep 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