| CPSC 120 | Principles of Computer Science Using Multimedia Design |
Fall 2008 |
In this lab, we will do more picture transformations that modify pixels in a range (as opposed to all of the pixels in a picture). You will use for loops similiar to those in the commands below, which remove the red component from the colors of the pixels in the left half of picture pict:
for x in range(1, getWidth(pict)/2):
for y in range(1, getHeight(pict)):
px = getPixel(pict, x, y)
r = 0
g = getGreen(px)
b = getBlue(px)
color = makeColor(r, g, b)
setColor(px, color)
You should now be fairly familiar with this type of looping after completing the previous lab, but review the textbook or lab 5 for more details if you are not.
Of course, as we have done in the previous labs, we will want to put commands into a function. For example, we could make a function called removeHalfRed:
def removeHalfRed(pict):
modPict = duplicatePicture(pict)
for x in range(1, getWidth(modPict)/2):
for y in range(1, getHeight(modPict)):
px = getPixel(modPict, x, y)
r = 0
g = getGreen(px)
b = getBlue(px)
color = makeColor(r, g, b)
setColor(px, color)
return modPict
This function takes as input a picture, duplicates it, removes the red component from the left half of the picture, and then returns the modified picture. You will be writing similar functions in this lab.
Important: be sure to follow the good programming style rules laid out in lab 3.
Here are the exercises for this week's lab. There are 3 required exercises and 1 extra credit (i.e., optional) exercise. 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 lab06 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.
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 lab06 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).
Write a function called rotate180, which rotates a picture 180 degrees. Here is how the transformation would look on a picture of me:
Important: you may not use the rotate90 function written in class. For instance, you may not call the rotate90 function (i.e., calling it twice) nor can you copy the code from the rotate90 function (copying it twice) into your rotate180 function. Instead, you must write a single function that transforms a picture to the 180 degree rotated picture all in one step. Your function should contain exactly two for loops, one inside the other and you should not call any non-built-in functions.
To write this function, you will have to spend some time thinking about where a pixel in the original picture is located in the new rotated picture. The analysis will be similar to the analysis we did when rotating 90 degrees in class. Look at the rotate90 function for help in writing your function, although you may not use this code as is (the transformation must be done in one step).
Write a new version of the scaleDown function from class, which takes a second parameter indicating the factor which the user wants to scale by. For example, newPict = scaleDown(pict, 2) would return a scaled down version of pict with exactly half the width and half the height. newPict = scaleDown(pict, 3) would also return a scaled down version of pict, but with a third of the width and a third of the height.
Here is a picture of me scaled down by a factor of 3:
As in the previous labs, 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 within your www folder (not your lab02 folder!). Here is my version of cs120.html. Your page should look similar.
For extra credit (i.e., optional), blend two pictures together with different dimensions. The first of the two pictures should have a larger height than the second picture (but a smaller width). The second of the two pictures should have a larger width than the first picture (but a smaller height). Part of each picture should be blended with the other picture and the other part should not be blended. For example, here is an example of a blending using two pictures of my dogs:
![]() |
![]() |
=====> | ![]() |
|
|