| CPSC 120 | Principles of Computer Science Using Multimedia Design |
Fall 2008 |
In this lab, we will perform various transformations on each pixel in a picture. Some example transformations include negating the color of each pixel (i.e., to build the negative picture) and converting the color of each pixel to gray scale.
To modify each pixel, we will use a loop, which is a feature in programming that allows the programmer to repeat some set of commands. In this lab, we will use one particular type of loop called a for loop, and our for loops will loop over all the pixels in a particular picture. Each for loop will repeat a set of commands to transform, in some way, each pixel in the picture. For example, the following (uninteresting) for loop will set each pixel's color to black:
for px in getPixels(pict): setColor(px, black)
This code loops over the pixels in picture pict. Note: we could loop over the pixels in a different picture by replacing pict in getPixels(pict) with some other name of a picture. Each time the loop repeats, px will refer to the current pixel. The setColor command is within the for loop (i.e., it will be repeated) since it follows ':' and it is indented (note: commands that should be repeated must be indented and appear after ':'). It sets the current pixel, referred to by px, to black.
This loop would normally go within a function definition. For example, we could put it within a function called makeBlack, which sets all the pixels in a picture parameter called pict to black:
def makeBlack(pict):
modPict = duplicatePicture(pict)
for px in getPixels(modPict):
setColor(px, black)
return modPict
As stated in lab 3, we must indent the commands within the function. Therefore, the line that follows "def makeBlack..." is indented two spaces. The command within the for loop ("setColor..."), i.e., the command that is repeated, must be indented with respect to the for loop, so it must be indented twice, or four spaces. The return command is not part of the for loop, so it is only indented once, or two spaces.
The first command in makeBlack makes a copy of the parameter pict. This is to preserve the original picture as we are going to modify the picture in makeBlack. The for loop then repeats over each pixel in the duplicate copy of the picture, modPict. Within the for loop, each pixel is set to black. After the for loop completes, the modified picture is returned.
To remove the red component from each pixel's color, we could do the following:
def removeRed(pict):
modPict = duplicatePicture(pict)
for px in getPixels(modPict):
r = 0
g = getGreen(px)
b = getBlue(px)
color = makeColor(r, g, b)
setColor(px, color)
return modPict
Within the for loop in the function above, we get the green and blue components from the current pixel, px, via the functions getGreen and getBlue, and assign these to variables g and b (note: there is also a getRed function for getting the red component). We assign variable r to 0 since we will remove the red component. We then make a color with these three component variables using the function makeColor, and we assign the resulting color to the variable color. Finally, we set the color of px to the variable color.
This last function can be easily modified to do a great many transformations on pictures. For example, we could remove green or blue very easily by setting either g or b to 0. As we saw in class, we can also use this template to write functions for computing the negative of a picture or converting a picture to gray scale. You will use this template several times in this lab.
Put a comment at the beginning of the program file stating your name, the lab number, and any other relevant information.
Put a comment at the beginning of each function that explains what the function does and any assumptions it makes.
Use meaningful variable and function names which suggest the role of the variable and/or function in the program, while avoiding extremely long names. Single-letter names are rarely appropriate.
Use comments within functions for commands that are potentially complicated or hard to follow.
Here are the exercises for this week's lab. They are due in lab next Friday (at the start of lab). You should write the functions for the exercises below in a single Python file called exercises.py within the lab04 folder within your cs120 folder on your desktop. Each function should take as a parameter a picture, duplicate that picture, modify the duplicated picture in some specified way, and return the modified picture (the one exception is the function in 2iii, which takes two picture parameters, duplicates the first, modifies it, and returns it).
Make sure you comment all of your code as described above. This will be an important part of your grade. Important: make sure you provide a comment describing how the function should be called so that I can execute the function as you do. Also, make sure you put all pictures that you use in this lab in your lab04 folder so that I have access to them (you will also need to copy some of them to your wwww folder so that they can be put on your web page.
Extra credit is possible for particularly nifty picture transformations.
Write a function called transformPicture, which uses a for loop to transform the color of each pixel in some way. Use a transformation that was not shown in class (it can be similar to one to one shown in class, but it must be different in some way). Also, don't perform a transformation that you will be asked to do in the second exercise.
Apply this function to some picture and save the resulting picture using writePictureTo (see the help documentation for information on how to call writePictureTo).
Write the following three functions, which were shown in class (at least partially):


Apply each function to some picture and save the resulting picture using writePictureTo (see the help documentation for information on how to call writePictureTo).
As in lab 3, you will put all the pictures that you created in the earlier exercises on your cs120 web page (cs120.html). You will add them to the table in cs120.html. If you recall from lab 2 and lab 3, each row of this table will contain a different picture (or sound or movie) transformation. Within each row, you should have a cell describing the transformation, a cell with the original picture (if applicable), and a cell with the edited picture. You should also make one row betweeen your lab 3 pictures and your lab 4 pictures, that indicates where the lab 4 pictures begin in the table.
Here is my version of cs120.html. Your page should look similar. If you have trouble with this, look at the HTML code for my page by clicking on "View" in the browser and then selecting "View Source" (this might vary a little for different browsers, but there should still be some option for viewing the source HTML code).
|
|