Due: Tuesday, February 28, 2017, 11:59 pm
The main purpose of this lab is to give you the tools you need to complete an image-manipulation project. You will be using John Zelle's graphics package. Here is the reference guide.
import graphics import display import sys
You will need to convert your photo to the PPM format. One way to do that is by using the ImageMagick program called convert. It is installed on the lab computers. Here is an example of how to convert and shrink a JPG image to a PPM image.
convert myImage.jpg -scale 25% mySmallImage.ppm
The convert program is a very powerful application for manipulating images. If you want to find out more about it, check out the ImageMagick web site.
In general, if you want to convert an image of one type to an image of another type, you can use the convert function. For example, the following converts an image of type JPG to an image with the same name of type PPM.
convert imageA.jpg imageA.ppm
By swapping the suffixes, you can change in image of type PPM to an image of type JPG. If you use the .png suffix for the second image, the result will be of type PNG (good for web pages). PNGs and JPGs are the best formats for your writeup or for web pages.
The first thing we're going to do is create a simple program that will read image data from a file and display it in a window. We'll use the command line to specify the filename of the image to view.
Create a main function in your show.py file. Give it one argument, which will be the list of strings from the command line. One of these strings should be the filename of the image to open and view. The main function should do the following.
exit()function. Use the
len()function to test how many strings there are in the list.
Load image data from the file specified in the second string of
the main function parameter into a Pixmap. You can do this by calling
graphics.Pixmap() with the filename as the
argument and then assigning the result to a variable.
Any time we need to store, retain, or assign information, we need to
use an assignment statement. In this case, we want to store the
Pixmap object created by the graphics.Pixmap() function
call. Therefore, you need a variable on the left side of an
assignment and the
graphics.Pixmap() call on the right.
You need to pass the filename to the Pixmap function. The filename
will be the second element in the list of strings from the command
line, which is the main function parameter.
displayPixmapfunction in the display package to create a window and display the Pixmap. You went over this function in class. It takes two arguments, which are the Pixmap to display and the title to be displayed in the window. You could use the filename as the title of the window. The displayPixmap function returns a window reference, which you need to assign to a variable. That means the variable to hold the window reference must be on the left side of an assignment and the function call must be on the right.
getMousemethod of the window. That means you type the name of the variable holding the window reference, then
.getMouse()to call the method. This will wait for a mouse click in the window and then go on to the next instruction. If there is no next instruction, the program will terminate.
Below the main function, put a conditional call to it, using the method we learned last week. Pass the main function the list sys.argv.
if __name__ == "__main__": main(sys.argv)
Once complete, try out your show function.
pm = graphics.Pixmap( 'mypixmap.ppm' ) (r, g, b) = pm.getPixel( 42, 35 ) pm.setPixel( 42, 35, (b, g, r ) )
def swapRedBlue( pmap ): # loop over each row row_idx # loop over each column col_idx # get the r, g, b values of the pixel indexed by (col_idx, row_idx) # set the pixel indexed by (col_idx, row_idx) to the value (b, g, r) # return
You can pass whatever filename name you want to the save function. I'd recommend not having spaces in the name.
At the end of your filter.py file, put a call to your
test function behind a conditional that tests the value of the
__name__ variable, as we did last week.
if __name__ == "__main__": test( sys.argv )
Note that this is the only place in your filter.py file where you should use the sys.argv variable. If you are using it in any of your functions, then you are not making proper use of the function parameters.
When you are done, run your filter.py program, giving it an image filename as the argument. Then look at the result with your show program. Now, perhaps, you see why command line parameters are useful beasts.
When you are done with the lab exercises, you may begin Project 4.
© 2017 Ying Li. Page last modified: .