For this coding challenge you will be creating an image
transformation where a red filter will be applied to all
columns and rows that have an index
divisible by an input parameter space
(integer). You're required to create a copy
of the image before modifying it and ensure that
the original image remains unmodified.
MAKE SURE TO EXPLAIN CODE ROW BY
ROW*******************************************************************************************
There are 3 files in this section:
CSE8AImage.py - This file SHOULD NOT BE
CHANGED. It contains helper functions that you can use
while creating your filter and writing test cases.
final2.py - This file is where you will define the spaced_filter
function. To run this file, you should use the command 'python
final2.py' on the terminal. This might be helpful while debugging
your code.
final_test.py - This file is where you will do
your testing. This file contains one testing
function, and you should add more tests to help ensure your program
is working. To run this file, you can use the Run button.
Function Name: spaced_filter
Parameters:
img - The 2D list representation of an
image
space - The space between filtered columns and rows. Only
columns and rows with an index divisible by space should be
filtered
Return: A 2D list representation of the
image with the red
filter applied to columns and rows with an
index divisible by space (an integer). The returned
image must not be the original image - you
are required to make a copy of the
image and not change the original image.
Description: This filter applies the red
filter to columns and rows in the image that have an index
divisible by space. The red filter applied keeps only the red
component of the original pixel. For example, for a pixel (25, 220,
145), applying the red filter should output (25, 0, 0). The
function should not modify the original image.
Example Result #1:
Applying the filter to the cat image with space =
15
Before:
After:
Example Result #2:
Applying the filter to the cat image with space =
5
Before:
After:
Example Result #2:
Applying the filter to the cat image with space =
100
Before:
After:
After defining your function, try running the test file using
the run button or "python final_test.py". Note that if you use the
run button, the image will be output to your screen, but not be
saved to your images folder.
Writing Test Cases
As we have done previously in PAs, you are tasked with
writing 4 test cases using assert statements,
and 1 test function that operates on
hopper.jpg in final_test.py. The test function should be called
test_spaced_filter_2(), and apply the filter on the hopper.jpg
image with a space of 3. These test cases
should demonstrate that your spaced_filter function has the
expected behavior. An example of a test case is below:
img = [[(0, 0, 0), (128, 128, 128), (90, 40, 50)], [(64, 128,
255), (255, 255, 255), (100, 125, 255)]] space = 2 observed =
spaced_filter(img, space) expected = [[(0, 0, 0), (128, 0, 0), (90,
0, 0)], [(64, 0, 0), (255, 255, 255), (100, 0, 0)]] assert observed
== expected, f'Expected spaced_filter({img}, {space}) = {expected};
got {observed}'
To reiterate, you must:
Write 4 test cases using assert
statements (similar to the one shown above but you should use
different 2D list of tuples instead of the one given in the
example). For these 4 tests, it is enough to use simple 2D lists of
tuples as shown in this example. You should not use actual images
as input for these 4 tests.
Write a test
function test_spaced_filter_2 to load
the hopper.jpg image, apply your filter
to it for space 3, and then save the
image.
Private Test Cases
This problem has 7 private test cases. Make sure to thoroughly
test your code - just because you pass the 3 public cases does not
ensure that you will get full credit on this problem
BEFORE
For this coding challenge you will be creating an image transformation where a red filter will be applied to all columns
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am