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

MCS 275 Spring 2022 Homework 10 Solution

  • Course Instructor: David Dumas
  • Solutions prepared by: Johnny Joyce


  • Complete the problems below, which ask you to write Python scripts.
  • Upload your python code directly to gradescope, i.e. upload the .py files containing your work. (If you upload a screenshot or other file format, you won't get credit.)


This homework assignment must be submitted in Gradescope by Noon central time on Tuesday 29 March 2022.


Collaboration is prohibited, and you may only access resources (books, online, etc.) listed below.

Resources you may consult

These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.

Point distribution

This homework assignment has two problems. The grading breakdown is:

Points Item
2 Autograder
4 Problem 2
4 Problem 3
10 Total

The part marked "autograder" reflects points assigned to your submission based on some simple automated checks for Python syntax, etc.. The result of these checks is shown immediately after you submit.

What to do if you're stuck

Ask your instructor or TA a question by email, in office hours, or on discord.

No problem 1 as usual

Problem 2: RGB channel separator

Write a program that reads a color bitmap image (mode RGB) from a file, whose name is given as the first command line argument. It should then create three image files, red.png, green.png, and blue.png of the same size as the input file. Each of these should be a grayscale (mode L) image, with contents as follows:

  • The brightness of the pixel at (x,y) in red.png is equal to the red component of the pixel (x,y) in the input file
  • The brightness of the pixel at (x,y) in green.png is equal to the green component of the pixel (x,y) in the input file
  • The brightness of the pixel at (x,y) in blue.png is equal to the blue component of the pixel (x,y) in the input file Thus, for example, if the pixel at (20,22) in the input file has color (8,220,91), then the pixel in red.png at (20,22) has brightness 8, the pixel in green.png at (20,22) has brightness 220, and the pixel in blue.png at (20,22) has brightness 91.

Here is a sample of what the output would look like. If the input image is: sunflower


  • red.png would be: sunflower

  • green.png would be: sunflower

  • blue.png would be: sunflower

Save your program as hwk10prob2.py and upload it to Gradescope.


In [2]:
from PIL import Image

filename = "images/sunflower.png"

img = Image.open(filename)
w, h = img.size # width, height

# Create new greyscale images so we can fill them in
img_red = Image.new("L", (w,h))
img_blue = Image.new("L", (w,h))
img_green = Image.new("L", (w,h))

# Iterate over each pixel
for x in range(w):
    for y in range(h):
        r,g,b = img.getpixel((x,y)) # Get RGB values of current pixel
        # Put each RGB value of pixel into corresponding images
        img_red.putpixel((x,y), r)
        img_green.putpixel((x,y), g)
        img_blue.putpixel((x,y), b)


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)
[ 2  9 16 23 30]

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 the average (arithmetic mean) of 
# all the entries of the matrix
[ 14  91 266 539 910]

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.linspace(21,30,10).reshape(5,2).T + np.array([5,50,500,5000,-1])



Put your commented version of this in a file called hwk10prob3.py and upload it to gradescope.


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

# Arrange numbers 21, 22, 23, ..., 30 into matrix with 5 rows and 2 columns.
# Add 5 to each value in the first column, 
# add 50 to each value in the second column, 500 to third, 5000 to fourth, -1 to fifth.
B = np.linspace(21,30,10).reshape(5,2).T + np.array([5,50,500,5000,-1])

# Print the sum of all values in the third and fourth columns

# Take each value x from the first row and transform it 
# into e^(-0.1*x), then print the sum