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

Quiz 10

MCS 275 Spring 2021 - David Dumas

Instructions:

Deadline

This quiz must be submitted in Gradescope by 12:00pm CST on Tuesday, March 30, 2021.

Topic

This quiz covers the Python modules pillow and numpy as covered in Lectures 24--26.

Resources you are allowed to consult

Quizzes are INDIVIDUAL, closed book, and only allow access to specified resources. For this quiz you can access:

Because of spring break, it's been a while since you completed Worksheet 10. Reading through the solutions would probably be a good idea.

Point distribution

There are two problems on this quiz, numbered 2 and 3. The point breakdown is:

Points Item
3 autograder
4 problem 2
4 problem 3
11 total

No problem 1 (as usual)

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.

Problem 3: Explain three lines of code

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

To explain what is expected, here is an example. The following program makes a numpy array and prints some parts of it.

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 [78]:
# Add a detailed explanatory comment above each line of 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)
276
[[ 360  624  960 1368 1848 2400]
 [ 624  960 1368 1848 2400 3024]]

Put your documented version of the program in a file quiz10prob3.py and submit it to gradescope. (Remember to add the required header and a docstring so it confirms to the course coding standards.)

Revision history

  • 2021-03-28 Initial publication