Due Wednesday, November 16 at Midnight.
At the conclusion of this programming assignment, participants should be able to:
Before starting this programming assignment, participants should be able to:
Content used in this assignment is based upon information in the following sources:
The PPM image format is encoded in human-readable plain text. A PPM image has two main parts:
The header is always the top three uncommented lines in the file:
P3
4 4
255
The first line specifies the image encoding that is contained within the file. We will always use the "P3" specification. The second line specifies the number of pixel columns and rows present in the image. In this example, we have a 4 pixel by 4 pixel image. The final number indicates the maximum value for each red, green, and blue (RGB) element in the picture. We will always use a max value of 255.
Below the header is the body of the PPM file. Each pixel has a red, green, and blue value. For example, the body content of our 4x4 image might be:
255 0 0 0 255 0 0 0 255 255 255 255
0 0 0 255 0 0 0 255 0 0 0 255
255 0 255 0 0 0 255 0 0 0 255 0
0 255 255 255 0 255 0 0 0 255 0 0
With these values, the pixel in the first column and first row has a RGB value of (255, 0, 0), which equates to red. The pixel in the last column and the first row has a RGB value of (255, 255, 255), which equates to white. A blown-up image containing these values would look like:
Note: You can explore RGB color values at this website.
Download this PPM image of the NY sky line: ny.ppm. You can open the image with software such as Ifranview to view it as an image. You can also open it as a text file (in Spyder IDE or a text editor) to view it as plain text (useful for debugging your code!).
(Image from https://pixabay.com/en/new-york-skyline-manhattan-hudson-540807/)
Write a program to modify a PPM image in two ways:
To do this, the flow of your program should be as follows:
<color>
" where <color>
is "red", "green", or "blue"process_header(infile, outfile)
: Writes the first three lines of the input image to the output image (no need to modify these lines)process_body(infile, outfile, modification)
: Walks through each line in the body of the input image, modifies the RGB values according to modification
, and writes out the modified output image. For the modification
, process_body()
calls one of the following functions for each RGB triple in the line:negate()
: Accepts a single integer RGB value and the output file object. To negate the value, subtract 255 and take the absolute value of the result. Writes the result to the output file.high_contrast()
: Accepts a single integer RGB value and the output file object. To apply high contrast, if the value is greater than 127, set it to 255, otherwise set it to 0. Writes the result to the output file.gray_scale()
: Accepts three integer RGB values and the output file object. Inspect all three red, green, and blue values for each pixel. For each RGB triple, convert the values to the triplet's average. Writes the result to the output file.remove_color()
: Accepts three integer RGB values, a string representing the color to remove, and the output file object. For example, suppose the color to remove is green. This function sets all green values to 0 (green is the 2nd value in an RGB triple). Writes the result to the output file.Negate line example: 1 2 3 200 100 150
negated is 254 253 252 55 155 105
High contrast line example: 1 2 3 200 100 150
in high contrast is 0 0 0 255 255 255
Gray scale line example: 1 2 3 200 100 150
as gray scale is 2 2 2 150 150 150
.
Remove green line example: 1 2 3 200 100 150
without green is 1 0 3 200 0 150
Note: Other than strip()
, do not use any other string methods in your code.
Note: I recommend you add code to prompt the user last. You can debug faster if you hard code the file names and the command, rather than typing them in each time as you implement your code to perform the image modifications.
Here is an example run of the program:
Please enter the input file name: brelsford.ppm
Please enter the output file name: brelsford_negate.ppm
Modification commands include:
"negate"
"high contrast"
"gray scale"
"remove red"
"remove green"
"remove blue"
Please enter the modification to perform: negate
Image modification "negate" complete. Closing files.
Here are the image files to compare with:
(ny.ppm):
Validate the input file specified by the user is valid, i.e. the input file adheres to the following requirements:
If the user specifies an invalid input file, tell the user why the file is invalid, and re-prompt until a valid file is specified by the user.
Hint: To check if a file exists, import the os
module and call the predicate function os.path.isfile(<string file name>)
:
import os.path
print(os.path.isfile("ny.ppm"))
print(os.path.isfile("file_that_doesnt_exist.ppm"))
<your last name>_pa6.zip
by the due date and time.This assignment is worth 100 points + 5 points bonus. Your assignment will be evaluated based on a successful compilation and adherence to the program requirements. We will grade according to the following criteria:
process_header()
process_body()
(and other helper functions you may choose to define)negate()
high_contrast()
gray_scale()
remove_color()