.pyfiles 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.
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.
This homework assignment has two problems. The grading breakdown is:
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.
Ask your instructor or TA a question by email, in office hours, or on discord.
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,
blue.png of the same size as the input file. Each of these should be a grayscale (mode L) image, with contents as follows:
red.pngis equal to the red component of the pixel (x,y) in the input file
green.pngis equal to the green component of the pixel (x,y) in the input file
blue.pngis 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.pngat (20,22) has brightness 8, the pixel in
green.pngat (20,22) has brightness 220, and the pixel in
blue.pngat (20,22) has brightness 91.
Here is a sample of what the output would look like. If the input image is:
red.png would be:
green.png would be:
blue.png would be:
Save your program as
hwk10prob2.py and upload it to Gradescope.
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) img_red.save("red.png") img_green.save("green.png") img_blue.save("blue.png")
For this quiz problem, you need to add explanatory comments to a given code sample.
# 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.
# 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:
# document this code import numpy as np B = np.linspace(21,30,10).reshape(5,2).T + np.array([5,50,500,5000,-1]) print(np.sum(B[:,2:-1])) print(np.sum(np.exp(-0.1*B)))
Put your commented version of this in a file called
hwk10prob3.py and upload it to gradescope.
# 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 print(np.sum(B[:,2:-1])) # Take each value x from the first row and transform it # into e^(-0.1*x), then print the sum print(np.sum(np.exp(-0.1*B)))