Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Task 7 : Changing brightness / contrast of an image: 2 0 points We can write a function that can change the brightness of an

Task 7: Changing brightness/contrast of an image: 20 points
We can write a function that can change the brightness of an rgb image. There are a lot of Python libraries that can do that. We will try to create a function that accepts an RGB image and a scale factor. In simple words you can think of changing brightness of an image as increasing or decreasing the pixel intensities by a factor.
We will create this function and it should only accept scale factor values between 0-1, any value greater than 1 causes an overflow exception which is beyond the scope of this project. But we will try and catch this exception.
Here are the detailed steps to creating and calling this function with exception handling, please follow the steps carefully:
The code for reading the image is given to you.
Create a function and name it rgb_brightness. This function accepts 2 parameters: rgb_image and factor.
Inside this function: if factor>1 or factor<0, just return don't do anything else. The if clause ends right here
Get the width, height and number of planes (it will be 3 for RGB image) from rgb_image.shape
Loop through the width and the height values (2 for loops, one within the other). Use x and y as the looping variables: eg: for x in range(width):
Inside the inner loop (after for y in range(height):, get the red, blue and green values for the current width and position in the variables r,g and b
Create 3 new variables: aug_r, aug_g, aug_b which are equal to the r, g and b values multiplied by factor. Also make sure that the final values are integers. (use int)
Create another variable: augmented_image. Now you will write 3 code statements that each assign the aug_r, aug_g and aug_b values to the current pixels AND plane to augmented_image.
return augmented_image
Now we want to call this function but let's include exception handling to make sure that when factor is greater than 1 or less than 0, the exception is catched without throwing an error:
The try and except blocks are aredy given to you. The code inside the try block is the same as other function calling blocks that were given to you.
Inside the try block, call the rgb_brightness function with img_arr and factor value of 0.5. Return the image in the variable: augmented
Convert augmented to a numpy array
Make sure augmnted is of type int
The next two lines of code are already given to you
Inside the except block: print: 'The factor value you entered is invalid. Please enter a value between 0 and 1'

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions