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
heightis out of bounds which means the givenheightparameter when added to the row index given in thetop_lefttuple is greater than the total number of rows present in the 2D list.The given
widthis out of bounds which means the givenwidthparameter when added to the column index given in thetop_lefttuple is greater than the total number of columns present in the 2D list.If
heightorwidthor the elements of the tupletop_leftare 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.