A document from MCS 275 Spring 2021, instructor Emily Dumas. You can also get the notebook file.

Quiz 10 Solutions

MCS 275 Spring 2021 - Emily Dumas

Solutions by Jennifer Vaccaro

Problem 2: Red to blue

Write a program that reads a color bitmap image (mode RGB) from a file, finds all pixels that are pure bright red (value (255,0,0)), and changes such pixels to pure bright blue (value (0,0,255)). All other pixels should remain unchanged. The modified image should then be saved to an output file.

The program should take two command line arguments:

  • Argument 1: The name of the input image file
  • Argument 2: The name of the output image file

In case you'd like to test your program, here are a pair of images that provide sample input and expected output. You'll want to download them and save them wherever you work on your quiz code.

Save your program to quiz10prob2.py and upload it to Gradescope.

In [2]:
# Quiz 10 Problem 2
# Jennifer Vaccaro
# I completed this work myself, in accordance with the rules in the syllabus.
"""Reads a PNG image from a file, converts the red pixels in it to blue, then saves the image to an output file."""

import sys
from PIL import Image

# Define the filenames and the relevant rgb values
infile = sys.argv[1]
outfile = sys.argv[2]
red = (255,0,0)
blue = (0,0,255)

# Read the image, read the size
img = Image.open(infile)
X,Y = img.size

#Iterate through the pixels. If any are red, then convert them to blue
for x in range(X):
    for y in range(Y):
        rgb = img.getpixel((x,y))
        if rgb == red:
            img.putpixel((x,y),blue)

# Save the image object
img.save(outfile)

Problem 3: Explain three lines of code

For this quiz problem, you need to add explanatory comments to a given code sample.

In [16]:
# Undocumented sample
import numpy as np

A = np.arange(35).reshape(5,7)
print(A[:,2])
print(A[4,3])
[ 2  9 16 23 30]
31

Here is a documented version of the same program, where every line of code is explained by a comment above it.

In [86]:
# Documented sample
import numpy as np

# Arrange the integers 0...34 in a matrix
# with 5 rows and 7 columns.  Square each
# one and add 10.
A = np.arange(35).reshape(5,7)**2 + 10

# Print column 2 of the matrix
print(A[:,2])

# Print the average (arithmetic mean) of 
# all the entries of the matrix
print(np.mean(A))
[ 14  91 266 539 910]
401.0

Notice that each comment is detailed enough that if you were only given the contents of the comment, you would be able to re-create the corresponding code. (In normal programming practice you wouldn't use this many comments, nor include this much detail, but I am showing what is expected in this problem.)

Add the same kind of explanatory comments to the following program:

In [ ]:
# document this code
import numpy as np

B = np.arange(13,43,5) + np.arange(30).reshape(5,6)

print(np.sum(B[::2,2::3]))

print(B[1:3]**2 - 1)
In [1]:
# Quiz 10 Problem 3
# Jennifer Vaccaro
# I documented this code myself, in accordance with the rules in the syllabus.

# import numpy with the usual alias
import numpy as np

# Creates a numpy array B by aranging the integers 0,...,29 in a 5x6 matrix, 
# then adding the numpy vector 13,28,...,38 to each row. 
B = np.arange(13,43,5) + np.arange(30).reshape(5,6)

# Print the sum of all elts of B which fall in even rows and columns with  
# indices congruent to 2 (mod 3).
print(np.sum(B[::2,2::3]))

# Print the array created by elt-wise squaring the 1st and 2nd rows of B, 
# then subtracting 1.
print(B[1:3]**2 - 1)
276
[[ 360  624  960 1368 1848 2400]
 [ 624  960 1368 1848 2400 3024]]