need an assignment done in python today

timer Asked: Mar 26th, 2017

Question Description

need to read file, check for problems in file, then open window and display photos with delay between

CS 101 - Assignment 5 -Slideshow Spring 2017 Algorithm Due : March 19, 2017 Program Due : March 26th, 2017 All work submitted must be your own. Deliverables : ​You only need to submit your solution. You should not submit the module. You must use functions to modularize your work. You should use exception handling where necessary as well. Image Slideshow You are going to write a python program to display and rotate through multiple images. The program will read a special type of text image file called PPMs, which of course have the extension .ppm. Once you have the images loaded you can show them one after another with a slight delay to let people view images in a particular directory. We’ll be using again, along with the Image class. PPM Files Portable PixMap format or PPM is a simple image file format. There are a few variants of the PPM format, but we will be concentrating on one particular specification of the text file versions. Since it’s just a normal text file, we can open, read and write the image files just like they were any normal text document. This makes it very convenient since we can use all the Python file handling techniques we’ve learned this semester. We have included ​ p ​ pmReader.html to view the ppm files that have been supplied. You may also want to use an image editor like gimp to view the images ( ​​). You will also want to examine the files in a text editor. PPM Format ( IMG_4833.ppm ) Line # Example Data Description 1 P3 PPM Header information. P followed by a number. We’ll be using the P3 format. 2 800 600 Size of the image in pixels. This 800 wide and 600 pixels tall. 3 255 Color Depth of the Image. Each Red, Green, Blue component will be 0-255 in value. This is always 255 in our examples. 4 169 Red Component of the first pixel. 5 157 Green component of the first pixel 6 135 Blue Component of the first pixel 7 170 Red component of the second pixel ... ... ... 144001 87 Red component of last pixel 144002 121 Green component of last pixel 144003 71 Blue Component for the last pixel. Graphics Forms IDLE doesn’t allow you to choose color that is shown in output, we’ll need to use a native OS window to display the information we want. In this project you’ll be given a module. Make sure it is in the same directory as your solution. You can import this python module just like any other built-in module. It will create a graphics window for your program. The following program will draw a Circle on the screen. Once you click on the window it will go away. >>> win = gfx.GraphWin("Test Image", 100, 100) >>> img = gfx.Image(gfx.Point(50, 50), 100, 100) >>> img.draw(win) >>> for y in range(100): for x in range(100): red = x // 100 * 255 green = (100 - x)//100 * 255 blue = y // 100 * 255 color = gfx.color_rgb(red, green, blue) img.setPixel(x, y, color) Let’s take a look at that line by line. >>> import graphics as gfx This imports the graphics module as gfx. This allows us to give the module an alias so we can just type gfx instead of graphics each time we want to call a function in the module. >>> win = gfx.GraphWin("My Circle", 300, 300) This line creates the new window. You call the GraphWin function to create the new UI window and assign the reference of that window to a variable. GraphWin function has several arguments; the first is the name of the window, this will be put in the title bar of the window. The second and third arguments are the height and width of the window in pixels. >>> img = gfx.Image(gfx.Point(50, 50), 100, 100) Create an image with the center at 50, 50 and a width of 100 and a height of 100 >>> img.draw(win) This line will actually draw the Image. Notice that we are using the variable img for the image and passing in the window to draw the circle on. >>> for y in range(100): for x in range(100): red = x // 100 * 255 green = (100 - x)//100 * 255 blue = y // 100 * 255 color = gfx.color_rgb(red, green, blue) img.setPixel(x, y, color) The nested loops will loop through all the rows and columns in the image. red = x // 100 * 255 green = (100 - x)//100 * 255 blue = y // 100 * 255 Calculate values for red, green blue from 0 to 255 based on the coordinates on the screen. Red increases from left to right, while green decreases from left to right and blue increases from bottom to top. color = gfx.color_rgb(red, green, blue) We need to get a text string of the color in the form #rrggbb where the red, green and blue are in hexidecimal img.setPixel(x, y, color) Set the pixel at a given x, y coordinate to the color we’ve chosen. Make sure you experiment with creating a window, displaying text, getting key input, etc. Some other functions that you’ll want to use in the graphics module. Graphics Module Functions Image class img = gfx.Image(gfx.Point(50, 50), 100, 100) The image method takes the anchor point from the center of the image to place the image. The last two arguments are the width and height of image. So if you want the top left of the image at the top left of the window, you will want to set the anchor point to half the width and height. You’ll still want to call ​img.draw(win)​ to show the image in the window. You can call img.undraw()​ to hide the image. img.setPixel(x, y, color) Sets the pixel at x, y to a given color. The color should be in a text string given from gfx.color_rgb img.draw(win) Draws the image onto the window passed as an argument. img.undraw() Removes the image from the window. win.getKey() The window handle that you get when you create a new window has a getKey() method that will return the key the user pressed. It will wait until the user presses a key. So if the user presses R for red it will return R. ( Remember case matters ) win.checkMouse() This method call lets you know if the user clicked in a window since you last checked. If the value is None, then the user has not clicked the mouse. You’ll want to use this so that if the user clicks in the window it will pause the reading so they can take a quick break. Once they click again, it should continue showing the words. win.getMouse() This method is different from the above method in that it will wait until the user clicks. So the program will not continue win.isClosed() Returns a boolean, if the graphical window has been closed then it returns True, otherwise if it is still open it returns False. You’ll want to make sure the user doesn’t close the window prematurely. If you wait for input from getMouse and the user closes the window you will get a GraphicsError. You can use try except to catch this to know that the user closed the window. >>> >>> >>> >>> >>> >>> import graphics as gfx win = gfx.GraphWin("My Circle", 300, 300) c = gfx.Circle(gfx.Point(50,50), 10) c.draw(win) win.getMouse() # Pause to view result win.close() # Close window when done Try closing the window instead of clicking in the window. You’ll notice you get an error. Try using the try except to catch the exception. The exception is GraphicsError in the module so if you alias the module as gfx like above, then you want to use ​except gfx.GraphicsError​. Time Module We’ll want to use the time module to determine how long it takes for the user to complete a quiz. The time module is a built in module. >>> import time >>> start = time.time() >>> time.sleep(1.2) >>> end = time.time() >>> print(“The program slept for “, end - start, “seconds”) time.sleep(1.2) will pause our program for 1.2 seconds. In the above example our elapsed time should be approximately 1.2 seconds. Os Module import os We’ll want to use the os module to work with items on our drives and get information from the operating system. os.path.isdir(directory) Isdir will return a boolean. The directory passed as a string argument, the function returns true if that directory exists, False if it doesn’t. The directory passed can be a full path or a relative path. os.path.join( path, *paths) You can pass multiple strings to os.path.join and it will join them by the default separator for your operating system. This allows your code to run the same on windows, mac, or linux. It returns a string of the arguments passed concatenated by the system separator. Sample Program Input a directory that contains ppm images => ​invalid_directory You did not supply a valid directory Input a directory that contains ppm images => ​bad_bitdepth PPM Images must have a color depth of 255 Input a directory that contains ppm images => ​bad_no_images This directory does not contain any ppm files. Please select another one Input a directory that contains ppm images => ​bad_p3 PPM images must begin with P3 in the header Input a directory that contains ppm images => ​bad_sizes All images in the directory must have the same size Input a directory that contains ppm images => ​umkc >>> Specification ● The user must choose a directory that contains ppm images, if they choose an invalid directory, or the directory doesn’t contain any .ppm images then warn the user and ask for input again. ● The ppm files in the directory must be valid. If the header doesn’t contain P3 as the first line in all the files then warn the user and ask for another directory. If the bit depth isn’t 255 for all the files, or if the sizes of the images in the directory aren’t the same size. ● Once the images are loaded pause 2 seconds between each one. Once you’ve shown all the images start from the beginning of the images again. ● Loading the images will be a little slow, so you don’t want to load the images each time you display them. Most likely you want to load all of them into the Image from the graphics library. Point Breakdown - May be modified as needed Points Requirement 5 Header 10 Readability, variable naming, comments 2 Warn user chooses a directory that does not exists 2 Warn user if directory has no ppm images. 3 Warn user if one or more of the ppm images does not have a P3 in the first line of the header 3 Warn user if one or more of the ppm images does not have 255 as their bitdepth 3 Warn user if all of the images in the directory are not the same size. 8 Load ppm file into graphics Image Appropriately 8 Show Image in graphics window 8 Rotate through all the images with 2 second pause between each 8 Recover from exception if the user closes the window 30 points off for programs that crash on expected input.

This question has not been answered.

Create a free account to get help with this and any other question!

Brown University

1271 Tutors

California Institute of Technology

2131 Tutors

Carnegie Mellon University

982 Tutors

Columbia University

1256 Tutors

Dartmouth University

2113 Tutors

Emory University

2279 Tutors

Harvard University

599 Tutors

Massachusetts Institute of Technology

2319 Tutors

New York University

1645 Tutors

Notre Dam University

1911 Tutors

Oklahoma University

2122 Tutors

Pennsylvania State University

932 Tutors

Princeton University

1211 Tutors

Stanford University

983 Tutors

University of California

1282 Tutors

Oxford University

123 Tutors

Yale University

2325 Tutors