Programming Problem Set #2
You must complete four problems in any combination of numbered exercises and challenges. Check-ins will begin around 20:00 the night the problem set is due.
Starter files available here. Download them, right-click on the zip file, and select “Extract All” to unzip. Then open the programming-2-starter folder in VS Code.
Installing Python Libraries
Open a terminal in VSCode (Terminal->New Terminal), paste in the following command, and hit enter.python -m pip install numpy matplotlib astropy vpython --no-warn-script-location
If you’re getting errors with files not being found when running your code, there is a VS Code setting you can change that will probably fix them.
Go to File->Preferences->Settings and enter python.terminal.executeInFileDir
in the search box at the top. A single setting should come up that looks like this:

Check that box, and you should be good to go.
Background
In these exercises you will apply your knowledge of lists and loops to the task of image processing. First, some background. You can think of a digital image as a large two-dimensional array of pixels, each of which has a red (R) value, a green (G) value, and a blue (B) value. An R, G, or B value is just decimal number between 0 and 1. To modify the image, you need to loop through all of these pixels, one at a time, and do something to some or all of the pixels. For example, to remove all of the green color from each pixel:
for every row in the image:
for every column in the image:
set to zero the green value of the pixel in this row and this column
The files for this lab are contained in programming-2-starter.zip. Download and unzip it to get started. These files are
fruit.py
: you will modify this file to implement your solution to exercise 0 (Fruit Ninja Academy)gray.py
: you will modify this file to implement your solution to exercise 1 (Grayscale)blue_screen.py
: you will modify this file to implement your solution to exercise 2 (Blue Screening)blur.py
: you will modify this file to implement your solution to exercise 3 (Blur)beach_portrait.png
: a picture of some backpacking goofball, used as the input image forgray.py
oz_bluescreen.png
: a still photo from the set of the 2013 film Oz the Great and Powerful, used as one of the input images forblue_screen.py
meadow.png
: a peaceful meadow scene, used as one of the input images forblue_screen.py
reference-beach_portrait_gray.png
: the expected output image forgray.py
, provided for your referencereference-oz_meadow.png
: the expected output image forblue_screen.py
, provided for your referencereference-beach_portrait_blur.png
: the expected output image forblur.py
, provided for your reference
There are additional files related to the optional challenges as well.
0. Fruit Ninja Academy
Congratulations! The time has come for your final exam at the Fruit Ninja Academy. The array of fruits you will use to demonstrate your slicing prowess has already been assembled in the starter code. You will now face a series of slicing challenges. Extend the provided fruit.py
to accomplish the following tasks, printing out the result of each task. The expected number of commands (not necessarily including printing) and expected output are given for each task.
- Extract the bottom right element in one command.
Tomato
- Extract the inner 2×2 square in one command.
[['Grapefruit' 'Kumquat']
['Orange' 'Tangerine']] - Extract the first and third rows in one command.
[['Apple' 'Banana' 'Blueberry' 'Cherry']
['Nectarine' 'Orange' 'Tangerine' 'Pomegranate']] - Extract the inner 2×2 square flipped vertically and horizontally in one command.
[['Tangerine' 'Orange']
['Kumquat' 'Grapefruit']] - Swap the first and last columns in three commands. Hint: make a copy of an array using the
copy()
method.[['Cherry' 'Banana' 'Blueberry' 'Apple']
['Mango' 'Grapefruit' 'Kumquat' 'Coconut']
['Pomegranate' 'Orange' 'Tangerine' 'Nectarine']
['Tomato' 'Raspberry' 'Strawberry' 'Lemon']] - Replace every element with the string
"SLICED!"
in one command.[['SLICED!' 'SLICED!' 'SLICED!' 'SLICED!']
['SLICED!' 'SLICED!' 'SLICED!' 'SLICED!']
['SLICED!' 'SLICED!' 'SLICED!' 'SLICED!']
['SLICED!' 'SLICED!' 'SLICED!' 'SLICED!']]
1. Grayscale
Modify gray.py
to convert image
to a black-and-white or grayscale version of the original. In the RGB color model, gray tones are produced when the values of red, green, and blue are all equal. A simple way to do this conversion is to set the red, green, and blue values for each pixel to the average of those values in the original image. That is Rgray=Ggray=Bgray=
. When you have implemented a correct solution, running gray.py
should produce a file called beach_portrat_gray.png
that is the same image as reference-beach_portrait_gray.png
:

2. Blue Screening
Movies–particularly (non-animated) action movies that use a lot of special effects–often use a technique called blue screening to generate some scenes. The actors in a scene are filmed as they perform in front of a blue screen and, later, the special-effects crew removes the blue from the scene and replaces it with another background (an ocean, a skyline, the Carleton campus). The same technique is used for television weather reports–the meteorologist is not really gesturing at cold fronts and snow storms, just at a blank screen. (Sometimes green screening is used instead; the choice depends on things like the skin tone of the star.) This problem asks you to do something similar.
We can combine an image of, say, James Franco on the set of Oz the Great and Powerful (oz_bluescreen.png
) with an image of scenery (meadow.png
) by replacing the bluish pixels in the first picture with pixels from a background picture. To do this, we have to figure out which pixels are bluish (and can be changed) and which ones correspond to the actor and props (and should be left alone). Identifying which pixels are sufficiently blue is tricky. Here’s an approach that works reasonably well here: count any pixel that satisfies the following formula as “blue” for the purposes of blue screening: B > R + G.
Modify blue_screen.py
to use this formula to replace the appropriate pixels in image
with pixels from background
. When you have implemented a correct solution, running blue_screen.py
should produce a file called oz_meadow.png
that is the same image as reference-oz_meadow.png
.