Lab time

There are 3 exercises below. Take some time to think through each exercise - think about what you need to do, and how you will get the computer to do it. Be sure to ask questions along the way, and play with your programs, making them slightly different, as you think of other ideas for altering pictures. If you find you are getting frustrated, you can always go to the HW link, and check out the solutions to these lab exercises. It is always a good idea to make yourself come up with your own idea first - you will learn more that way - but don't let yourself get bogged down either.

1.0 Changing the amount of blue in a picture

We discussed the text containing some examples for decreasing the amount of red, green and blue at every pixel in a picture. The amount that the color value is changed, in these examples, is fixed, in the commands in the functions. It might be more useful to have a function that can change the amount of a color, either increasing or decreasing it, depending upon what you want to do to a given picture.

Your first task in this lab is to write a function, called changeBlue(picture, amount), that will change the value of blue in every pixel in a picture. For this function, you will practice using a 'for' loop, and will also need to think about what commands you need to execute first, in the command area, before you are ready to call your function (i.e., you will need to select a picture file, and make it into a picture, before you can pass it to your function.)

Recall the syntax for a 'for' loop is:

for <variable> in <sequence>:

<body>

where the <variable> is a name you assign, the <sequence> is a collection of data values or objects which will be assigned, one after the other, to <variable>, and <body> is your group of (indented) commands to be executed. You must have the words 'for' and 'in' and you must include the colon ':' at the end of the first line.

For this task, the input to your function will be (1) the picture to be changed, and (2) the amount by which to change the blue value (to decrease the blue, pass in a value less than 1, such as 0.5, to increase the blue, pass a value greater than 1. What happens if you pass in a very large number?). Write a 'for' loop inside your function to change the blue value of every pixel. Finally, display your resulting picture. After you save your function, be sure to remember to 'load' it into the command area.

The example function doNothing given below is a good place to start for this exercise. The function does not modify the picture at all, but simply takes every pixel of the image and sets its red component to whatever value it already is set at:

def doNothing( picture ):

for pixel in getPixels( picture ):

value = getRed( pixel )

setRed( pixel, value )

Examples of how your function might affect pictures are to the right, with the top picture being the original picture, and then with the amount of blue increased by 50%, and then decreased by 50%. What happens to your picture if you repeatedly increase the blue, as compared to what happens if you repeatedly decrease the blue?

If you want to save your pictures, you can use the following commands. Note that for these commands to work, I have already defined a 'picture' object to the variable 'mtn.' The 'setMediaPath()' command will pop up a dialog box for you to select the default path for your pictures. You can either use this function or type in the complete path name + file name as the second parameter to the function writePictureTo(). The 'Note' at the end of the commands below is letting me know that the file mtn_blue.jpg did not previously exist, and so is being created. If you do not get this message, is means your .jpg did previously exist, and has now been written over.

> mtn = makePicture(pickAFile())

> setMediaPath()

New media folder: H:\CSC 111\Sample Photos\

> writePictureTo(mtn,getMediaPath("mtn_blue.jpg"))

Note: There is no file at H:\CSC 111\Sample Photos\mtn_blue.jpg

Alternatively, you can type the full path plus file name into writePictureTo(), rather than using the setMediaPath() function. To do this you would type

> mtn = makePicture(pickAFile())

> writePictureTo(mtn,"H:\CSC 111\Sample Photos\mtn_blue.jpg")

Note: There is no file at H:\CSC 111\Sample Photos\mtn_blue.jpg

If you are having trouble with the backslashes '\', you might want to define a string variable to use in writePictureTo(). A final comment - you will most likely need to save your file to the hard drive of the computer you are working on, or to a jump drive, and then copy the picture to the class drop box. A number of students are having trouble writing pictures directly to the drop box.

2.0 Making Dusk Fall

For the second program today, you can start with the text example called 'makeSunset.py' in which the red in a picture is enhanced by decreasing the amount of blue and green in every pixel. This is an example of writing functions that do one and only one thing - so that they are general and reusable. This program has an upper level function, which calls two other functions, each of which does one thing. Type in this example, and play with it, to be comfortable with how it works.

# makeSunset.py

# Class example, Feb 3, 2009

# This function simulates a sunset by decreasing the

# green and blue of each pixel

def makeSunset(picture):

decrBlue(picture)

decrGreen(picture)

show(picture)

def decrBlue(pict1):

for p in getPixels(pict1):

value = getBlue(p)

setBlue(p, value*0.7)

def decrGreen(pict2):

for p in getPixels(pict2):

value = getGreen(p)

setGreen(p, value*0.7)

Note in this program, the 'picture' parameter in each function is given a different name. You can either use the same name, or different ones - the variables and parameters in different functions are distinct from each other. It can be useful to use different names as your programs become more complex, so that it is easier to identify where your problems are occurring.

Now, write a program called makeDusk() that decreases the amount of green and red in the picture you pass in, rather than changing the amount of green and blue. Test your program to make sure it behaves as you expect. What happens to your picture if you call makeDusk() repeatedly with the same picture?

The picture at right shows the results after calling makeDusk(picture) twice.

3.0 Adding a Yellow Grid

For the third lab program, you will practice using a 'for' loop and also editing an existing program. From class, we saw a program that draws 5 vertical yellow lines across a picture. This function is:

# yellowLines.py

# Class Example and Lab 2, Feb 3, 2009

# Function draws 5 vertical yellow lines on the picture

def yellowLines(pict):

num = 5 # draw 5 vertical, yellow lines

H = getHeight(pict) # get the height of the picture in pixels

W = getWidth(pict) # get the width of the picture in pixels

lineSpac = W/(num + 1) # determine the spacing of the lines

x = lineSpac # position of the first line

for y in range(1, H):

setColor(getPixel(pict, x, y), yellow)

setColor(getPixel(pict, x + lineSpac, y), yellow)

setColor(getPixel(pict, x + 2*lineSpac, y), yellow)

setColor(getPixel(pict, x + 3*lineSpac, y), yellow)

setColor(getPixel(pict, x + 4*lineSpac, y), yellow)

show(pict)

For this exercise, you want to first change this program so that it draws only 4 equally spaced lines. This editing exercise will make sure that you understand how the 5 lines are currently being drawn, and that you know how to change this to make only 4 equally spaced lines. The next step is to add code that will draw 4 horizontal lines in addition to the 4 vertical lines.

The top picture at the right shows the result of running the class example. The bottom picture shows the results from running a function such as you will write now for lab.

Note that we will soon learn how to make nested 'for' loops - this exercise will help motivate that skill.