Handling edge cases#
Now that we have seen how to write the function for applying the red filter to a specific region in an image, let us see how we can handle few edge cases that arise depending on the given input parameters. We will only focus on the following edge cases:
The given
height
is out of bounds which means the givenheight
parameter when added to the row index given in thetop_left
tuple is greater than the total number of rows present in the 2D list.The given
width
is out of bounds which means the givenwidth
parameter when added to the column index given in thetop_left
tuple is greater than the total number of columns present in the 2D list.If
height
orwidth
or the elements of the tupletop_left
are negative, it is considered as invalid input.
The way we will handle these cases is returning a message to the user either saying “Input parameter out of bounds” or “Invalid input”.
So the function will now be modified as follows:
Sample 1#
Then the output from the function would be:
Input parameter out of bounds
This is because top_left[1] + width
would give us 1+2=3 which is greater than the number of columns present in the given 2 Dimensional list.
Sample 2#
Invalid input
This is because one of the input parameters height
is negative.
Sample 3#
Then the output from the function would be:
Input parameter out of bounds
This is because top_left[0] + height
would give us 0+3=3 which is greater than the number of rows present in the given 2D list.