Attached below are all the requirements for the coding task. This task is a two-parter, one being the lab 5 assignment and the other is the exam assignment (which implements lab 5 code into the...


Attached below are all the requirements for the coding task. This task is a two-parter, one being the lab 5 assignment and the other is the exam assignment (which implements lab 5 code into the assignment). Overall once completed it should come up to 5 .py as well as 2 .txt files altogether. In addition to that, all .txt files should answer all given question and be


formatted as described in the attached text file format.






For the exam assignment please disregard .pdf file photocopy and complete given written questions as a .txt file instead. I want to make it clear that I need these files as soon as possible but no later than May 5 at 12pm. And to make sure you


follow all requirements.




__MACOSX/._Exam Exam/exam 4.png __MACOSX/Exam/._exam 4.png Exam/exam 3.png __MACOSX/Exam/._exam 3.png Exam/exam 2.png __MACOSX/Exam/._exam 2.png Exam/exam 1.png __MACOSX/Exam/._exam 1.png __MACOSX/._Lab 5 Lab 5/lab5.pdf CISC 120: Lab 5 Contents 1 Introduction 1 2 Tackling the Problem 2 3 Submission 12 4 Rubric 13 1 Introduction This programming lab is designed to test your understanding of working with Python ob- jects and methods. It involves a simple image manipulation program, which will utilize the booksite Picture class. Compose a program zoom.py that takes the name of a picture file and three floats: s, x, and y, as commandline arguments and displays a picture that zooms in on a portion of the input picture. The input s should be a floating point number representing how much to zoom in. For example, if s == 2, the program should show an image with a 2x zoom applied. The other inputs should be between 0 and 1, and should represent the relative (x, y) coordinates of the center of the region to be zoomed in on. 1 2 Tackling the Problem Decomposing the Process At first glance, this problem will likely appear terrifying. This is completely normal, the problem is in fact quite challenging. However, this problem serves to illustrate a very important concept when approaching programming problems. The high level problem– implementing digital zoom–is very complex. If you were to attempt a direct approach from this level of analysis, you would have an uphill struggle ahead of you. However, the problem actually becomes quite easy if you break it apart in the correct way. It takes a long time to develop this sort of thinking, and so here I will spell it out for you. In time, and with practice, you’ll gain the ability to break problems down like this yourself. The challenging problem of digital zoom can actually be viewed as two smaller problems, one of which you already know how to solve, and the second of which is not terribly complex (at least in comparison to digital zoom). These sub-problems, if you will, are: 1. Take the input image and scale it up by the specified zoom factor. For example, if you wanted a 2x zoom, your first step would be to scale the image to have twice the height and width. 2. From the scaled image, create a new image by using all pixels within a rectangle centered at the specified point, and of the same size as the original, pre-scaled image. To see how this works, let’s use a specific example. Consider the image shown in Figure 1. This is JW Waterhouse’s painting, The Sorceress, which depicts Circe, a character from Homer’s Odyssey. Let’s see if we can zoom in on Circe’s face. Unfortunately, due to the limitations of needing to fit everything nicely on a page, the images you see in this document have been rescaled to fit on a sheet of paper. However, the raw image files for each figure are available on Moodle, and I recommend that you download and look at these as we go along. Page 2 Figure 1: Example image: The Sorceress, JW Waterhouse, 1913 Page 3 Scaling the Image The first thing that we need to do is write a program that scales the image to be double its existing size. In the interest of making our end goal, a fully general zoom program, as easy as possible, we will implement our scaling functionality as a function–that way we can simply use it directly, and without modification, in our final program. Sedgewick provides a short program which performs image scaling, which you can find, along with a description of its functionality, on pages 373 and 376. I suggest that you read over these pages, if you’ve not already. But, for convenience, I will copy the program here. import sys import stddraw from picture import Picture fil = sys.argv[1] wT = int(sys.argv[2]) hT = int(sys.argv[3]) source = Picture(fil) target = Picture(wT, hT) for colT in range(wT): for rowT in range(hT): colS = colT * source.width() // wT rowS = rowT * source.height() // hT target.set(colT, rowT, source.get(colS, rowS)) stddraw.setCanvasSize(wT, hT) stddraw.picture(target) stddraw.show() Based on this code, compose a function with the following definition: def scale(image, scale_factor): that accepts a Picture object (not a string, pass the actual Picture object) and a floating point scale factor, and returns a new Picture object that contains the image contained in the argument, but with its dimensions scaled by the specified factor. For example, if a 500-by-500 pixel image is used as input, and a scale factor of 2.0 is specified, the returned Picture object should be 1000-by-1000 pixels in size. Once this function has been defined, write a main function that takes a filename and a scale factor as command-line arguments, and then calls the scale function using those inputs, and then displays the resulting scaled image. Call this file scale_test.py. 20pts. Page 4 Drawing the Rectangle Once the image has been scaled, we will need to select a rectangular portion of the scaled image, and use this to create our zoomed image. We’ll start by just taking the scaled-up image, and drawing a rectangle over top of it. Then, once we can locate the appropriate rectangular area, we can take a look at copying that region into a new Picture object. Our final digital zoom program will need to define this rectangle using two floats, on the range [0, 1], and so we will do the same here. Just to make life a little easier later. The interpretation of these two floats is that they provide the relative location of the x and y coordinates of the center of the rectangle. For example, if we had our 500-by-500 image, and our center point were defined as .25, .50, then our rectangle’s center point would be located at (125, 250). The x-coordinate arises from finding the coordinate that is 25% of the way along the x axis (500 ∗ 0.25), and the y-coordinate from finding the coordinate 50% of the way along the y-axis (500 ∗ 0.50). Let’s begin by simply marking this point on our image using a dot. Create a copy of scale_test.py called draw_rect.py. Modify this program so that it accepts two more arguments, floats representing the relative coordinates of the center of the rectangle. Then, once the image has been scaled, use the stddraw module to place a dot at that point. To test it, use the sorceress.jpg file on Moodle. Scale it up by a factor of 2, and then draw a dot at the relative coordinates (.65, .70). The end result should resemble the image shown in Figure 2. WARNING 1 Don’t forget to set the x and y axis scales! The default scale is [0, 1], but your coordinates run from 0 up to the height/width of the image. You’ll need to use setXscale and setYscale to set up the canvas’s drawing scale appropriately. Next, we want to draw a rectangle centered on this dot. The relevant function you’ll want is stddraw.rectangle, which will accept the coordinates of the lower left corner of the rectangle, as well as its height and its width. For the height and width, simply use the height and width of the original picture. However, you will need to think a bit about how to find the lower left corner. You have calculated the center point (where you put the dot). It might help to use some graph paper and draw some rectangles. See if you can come up with the relevant formula to calculate the lower left corner of a rectangle, given its center point, length, and width. Once you have that figured out, modify draw_rect.py to draw the rectangle over top of the image. You should have an output that resembles Figure 3. 20pts. Page 5 Figure 2: The image with a dot roughly centered on Circe’s head. Page 6 Figure 3: The scaled image with a rectangle drawn. This rectangle represents the zoomed-in portion that you will want to extract. Page 7 Zoom Now, we can finally approach the problem creating a zoomed-in picture. The idea here is that you will want to write some code that takes each pixel within the region bounded by the rectangle that you drew above, and copy it into a new picture object, which will represent the zoomed-in image. Here you will encounter an immediate problem, though. The coordinate system for pixels in an image, and the coordinate system of the drawing canvas, are a bit different. And this will need to be accounted for. The pixels in a Picture object follow the standard coordinate system for computer graph- ics, which is one that we haven’t seen before. The origin of this coordinate system is not located in the lower left hand corner, but rather the upper left hand corner. This means that, relative to our drawing canvas, the y coordinates for pixels in our image will be flipped. Rather than a relative y coordinate of .8 representing a point 80% of the way up, towards the top, of the image, it will represent one 80% down, towards the bottom. Thus, when generating our coordinates to use for extracting the image, you’ll need to subtract the input numbers from 1. For example, if the program is given the following relative coordinates, (.6, .8), when calculating pixel indices in the picture itself, you will actually need to use the relative coordinates (.6, .2), because 1.0 − 0.8 = 0.2. With that complication in mind, write a function called extract_image, which accepts as input the scaled image, relative x and y coordinates, and the width and height of the original image (i.e., the exact same inputs as your rectangle drawing function needed). Then, create a program called zoom.py which uses scale and extract_image to zoom in on a specified region of an image. This program should accept as arguments the filename of an image to be zoomed in on, the scale factor (2 to zoom in 2x, etc.), and the relative x and y coordinates of the point to zoom in on. 30pts. Figure 4 shows the output that you should see from calling the program using the following arguments: % python zoom.py sorceress.jpg 2 .65 .70 Page 8 Figure 4: The output of zoom.py, a zoomed in version of the painting. Page 9 Analysis (a) Compare the output of zoom.py to the original picture.
Apr 27, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here