[Coding Exercise] Week 10 - Interactive image processor

pdf

School

Simon Fraser University *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Oct 30, 2023

Type

pdf

Pages

6

Uploaded by xiaofanfan

Report
7/17/22, 1:28 PM [Coding Exercise] Week 10 - Interactive image processor https://canvas.sfu.ca/courses/70584/assignments/783867?module_item_id=2570336 1/6 [Coding Exercise] Week 10 - Interactive image processor Due Jul 25 by 11:59p.m. Points 18 Submitting a file upload File Types zip Available until Jul 27 at 11:59p.m. Start Assignment You are going to process a very recent image captured by the James Webb telescope, released on July 12 2022!! Create an interactive program that performs 4 image manipulations on an image [ ngc3132.jpg ] (width = 600, height = 555) of the Southern Ring Nebula as seen by the James Webb Telescope. Credit: NASA, ESA, CSA, and STScI. The program must: Take user input (0/1/2/3/4) using a while-loop to determine which manipulation options (1/2/3/4) they want to apply. If the user enters 0, the program ends If the user enters 1/2/3 or 4, perform the manipulation, then save the resulting image as a new image under the name result-optionX.jpg where X is the user input option. For example, result- option1.jpg should contain the interlaced image. Once the manipulation and saving is done, the
7/17/22, 1:28 PM [Coding Exercise] Week 10 - Interactive image processor https://canvas.sfu.ca/courses/70584/assignments/783867?module_item_id=2570336 2/6 program asks the user for an input again. Optionally, the resulting image can also be shown to the user, but this is not required. If the user enters something other than the expected input (e.g., 37), display the message “Sorry, I don’t understand 37” and take the user input again. Each manipulation should be done on the original image. Define one function for each manipulation option : 1: interlace(pixels) – pixels is a parameter to the function, which will receive a 2D list of pixels representing an image, as seen in class. This function interlaces the image with black lines by blacking out (i.e. replacing every pixel with black) every second row . You should start with the first row (row 0). Sample output: Before After 2: invert(pixels) – inverts the colour of the image. To calculate the inverted colour of a pixel, its red, green, and blue components are replaced with 255-red, 255-green, and 255-blue , respectively. For example, if a pixel has [100, 150, 200] as its red, green, and blue components, its inverted colour will be [255-100, 255-150, 255-200], which is [155, 105, 55]. Sample output: Before After
7/17/22, 1:28 PM [Coding Exercise] Week 10 - Interactive image processor https://canvas.sfu.ca/courses/70584/assignments/783867?module_item_id=2570336 3/6 3: grayscale(pixels) – replaces all colours with shades of gray. To calculate the grayscale colour of a pixel, its red, green, and blue components are replaced with the average of these components . For example, if a pixel has [100, 150, 200] as its red, green, blue colour, the average is (100+150+200)/3 = 150. Then the grayscale colour of this pixel will be [150, 150, 150]. Sample output: Before After 4: saturation(pixels, factor) – adjusts the saturation of the image. This function should receive a numeric parameter, factor, as well as the 2D pixels list. To s aturate an image, the RGB values of a pixel are scaled by factor amount relative to its grayscale value . To perform this scaling for a pixel, first calculate its grayscale value gs as in the grayscale function above. Then, the red,
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
7/17/22, 1:28 PM [Coding Exercise] Week 10 - Interactive image processor https://canvas.sfu.ca/courses/70584/assignments/783867?module_item_id=2570336 4/6 green, and blue components are each replaced with gs + factor*(red - gs ) , gs + factor*(green - gs ) , and gs + factor*(blue - gs ) . For example, if a pixel has [100, 150, 200] as its red, green, blue colour, the grayscale value is (100+150+200)/3 = 150. Then if you scale the saturation by 0.5 the new colour of this pixel will be [150 + 0.5*(100 - 150), 150 + 0.5*(150 - 150), 150 + 0.5* (200 - 150)], which evaluates to [125, 150, 175]. Note: your program will need to ask the user to input a scaling factor to use as the factor argument to this function. Hint: you will need to deal with overflow. Overflow occurs if the any of the red, green, or blue values fall outside of the range 0 to 255. If the red, green, or blue value is less than 0, it should be set to 0. Likewise, if it is greater than 255, it should be set to 255. Sample output: Before After scaling by a factor of 0.5 Before After scaling by a factor of 2
7/17/22, 1:28 PM [Coding Exercise] Week 10 - Interactive image processor https://canvas.sfu.ca/courses/70584/assignments/783867?module_item_id=2570336 5/6 Requirements Use a nested for-loop in each function to apply changes to the pixels. Each function doing a manipulation should return a 2D pixel array representing the resulting image, and which could later be used as argument when calling the functions cmpt120Image.saveImage(...) and cmpt120Image.showImage(...) . You are required to use cmpt120Image.saveImage(...) to save each resulting image but you are NOT required to use showimage(...), as it may not work correctly in some computers (although, if it works, it can be practical to show to the user to aid debugging). Here is the sample output of a portion of the interactive program: Welcome to Image Processor! Here are your options 0: Quit 1: Interlace with black lines 2: Invert the colour of the image 3: Convert the colour of the image into grayscale 4: Adjust the saturation Enter your choice (0/1/2/3/4): 2 OK... Image saved as result-option2.jpg Enter your choice (0/1/2/3/4): 4 How much should I adjust the saturation by? 0.5 OK... Image saved as result-option4.jpg Remember to design your algorithm in English first, then translate it to Python code. Test as you go! Notes For this exercise you will be indirectly using an image processing library (pygame) which is not natively available in IDLE. You can choose to use Replit ( https://replit.com (https://replit.com/) ) to do this exercise, or use another Python code editor called Mu ( https://codewith.mu/ (https://codewith.mu/) ). Both Replit and mu have the library built-in and is ready for use. Mu might try to open a new window to show you an image but it might not work on some computers. If you have a non-responsive image you can stop the execution. We have provided you with 4 functions ( getImage , saveImage , showImage , and getBlackImage ) in a separate Python module file named cmpt120image.py (https://canvas.sfu.ca/courses/70584/files/19436954/download?download_frd=1) . (thsi is the same file we used in class). Use it to help you with this exercise. Do not change anything in this file . You must put all your function definitions for the image manipulation options (invert, grayscale, etc.) in another Python module file named myImageManip.py . Each function takes in a 2D list containing all the pixels (and possibly other parameters) which should have been returned from the getImage(...) function. Refer to our lectures for details. Your Python code should import the module cmpt120image. You cannot use other libraries to manipulate images.
7/17/22, 1:28 PM [Coding Exercise] Week 10 - Interactive image processor https://canvas.sfu.ca/courses/70584/assignments/783867?module_item_id=2570336 6/6 You must put your code for the interactive program (asking for user input and calling the correct functions) in a Python file named main.py . This file is what we called a “driver file” that calls functions from other imported modules including getImage, showImage, invert, grayscale, …etc. You may modify the 2D pixel lists as you do the manipulations in the manipulation functions, but make sure that you have an original image when you start any manipulation. Readability Guidelines Please check that your code follows the guidelines here: readability guidelines Submission Submit a .zip file containing your main.py , and your myImageManip.py . We will be marking your main.py and myImageManip.py files , using the same copy of cmpt120image.py as provided in this assignment. Make sure you also complete the separate Week 10 reflection activity TBA. Marking Notes If your program does not run (i.e. crashes or produces an error), your mark will be calculated using the rubric and then multiplied by 0.5. To avoid this penalty, try to finish early so you can get help from TAs if needed! It is better to submit code that works perfectly but doesn't contain all the features, than one that has all the features (potentially) but crashes. Each line of your code (including comments) should NOT exceed 100 characters. This is to help you develop a habit of good code formatting. In most cases you can break long code into separate steps, and some components that span multiple lines. You could also use intermediate variables to store parts of long messages to be printed (more situations will be seen as the course advances). You may use this sentence as a guide: # This line has exactly 100 characters (including the period), use it to keep each line under limit. *It is possible that in some operating systems or Python versions the showImage(...) function will not display the image properly. Try Replit and then Mu. If none works, just save the image with the name result-optionX.jpg, where X is the option number. This coding exercise does not require the use of the showImage(...) function.
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help

Browse Popular Homework Q&A

Q: Consider the same population of mice with two alleles for a digestive enzyme, E and e. Assume the…
Q: Find a counterexample for the statement. N = For every real number N > 0, there is some real number…
Q: Consider a loop branch that branches nine times in a row, then is not taken once. What is the…
Q: Consider a loop branch that branches nine times in a row, then is not taken once. What is the…
Q: 2. # of Absences (in weeks) Exam Grade 1 2 3 4 5 23 014 90 85 95 92 80 Correlation analysis of the…
Q: 2. A ship travels due west for 80 miles. It then travels in a northern direction for 59 miles and…
Q: Set up integrals for both orders of integration. Use the more convenient order to evaluate the…
Q: x2 − 4
Q: The Venn diagram shows sets A, B, C, and the universal set U. Shade Bn (C'UA) on the Venn diagram. U…
Q: the original mass of the iron nails was 100 grams after one week the mass was 120
Q: How does decreasing the total energy of the system affect the stability of the pair of atoms in the…
Q: winter day starting at sunrise. 5. The graph shows the outdoor temperature on a certain…
Q: Write a note on DOM manipulation.
Q: The domain for this problem is some unspecified collection of numbers. Consider the predicate P(x,…
Q: In the western United States, there are many dry land wheat farms that depend on winter snow and…
Q: s membrane C. How does the diagram inform your answer to the question? D. What is the answer to the…
Q: problem did the American independence leave them
Q: 2. A system consists of 1 mole of ideal gas is taken through the cycle shown below from (123). For…
Q: Write three statements to print the first three elements of array runTimes. Follow each statement…
Q: b. Writ For 6-7, write the equation that models each linear relationship. 6. ty 18 16 8642064NO 14…
Q: For the diagram shown below find the reactions at each support
Q: Upload a drawing of a graph showing the change in potential energy (on the y-axis) as two molecules…