CSI Lab 03

Tuesday, January 27th

Objectives:


Introduction

The material in this week's lab is based heavily on the material in section 3.3 of your textbook.  At times, we will ask you to work very closely with your book.  At other points, we will ask you to use what you have just done to write some code of your own.  If you get stuck, feel free to refer to the appropriate pages in your textbook. 

As in the past, the instructions are contained on this website, but the answers or signatures that you are required to obtain should be completed on the paper Activity Report which will be provided to you in lab.


Getting started

Suppose that you have been hired by the UNI athletic department to help them with some publicity.  They have two images - one is of the UNI mascot, TC, the other is of the football field in the dome - that they want to do some file manipulation with.

Save the following files to your media directory on the math-cs drive (P: drive).  You can either choose to save the following files or you can copy them from the common_media directory to your personal media directory as we did in lab1.

In order to view these files before you work with them, launch the image explorer tool by executing the following commands in the Interactions pane.

    > mascot = makePicture(pickAFile())
    > field = makePicture(pickAFile())

From the MediaTools menu in JES select the Picture tool and then select and explore each of these images.

mascot.jpg

[Q1]  What is the size of this image?

[Q2]  What is at location 180,75?

[Q3]  What position is the upper-leftmost corner of the "5" on his jersey?

[Q4]  Imagine a box surrounding the phrase "University of Northern Iowa"  What would be the approximate positions of the upper-left and lower-right corners of this box?

[Q5]  What are the RGB values of TC's fur?  Of the purple color at the bottom of the screen?

footballfield.jpg

[Q6]  What is the size of this image?

[Q7]  What is at location 180,75?

[Q8]  What are the RGB values of the "grass"?  Of the endzone? (For both of these, the values vary widely.  Pick a "typical" value.)


Activity A : Decreasing the Red in a picture

Suppose that the athletic department wants you to adjust the colors in these images.  For example, suppose that they feel that there is a little too much red in the picture of the picture of TC (there isn't, but just play along!).

Page 56  in your textbook contains Recipe 8 which is the decreaseRed() method for a Picture.

Type the code for recipe 8 into the program area (the white area at the top) of JES.  Be very careful and pay attention to details.  Remember that things such as spelling and certain kinds spacing in Python are VERY important.

When you are done, save this file to your directory on the P: drive as "lab04.py" and then load the program.  To test that it works properly run the following commands from the interactions pane:

    > mascot = makePicture( pickAFile() )
    > show(mascot)
    > decreaseRed(mascot)
    > repaint(mascot)

Please note that you will need to be patient when you run the decreaseRed() method.  This might take a while.  Trust that it will finish on its own.  If you receive errors, fix the problems.  If you have typed everything correctly you should notice a significant change to the picture.

[Q9]  What do you observe?

[Q10] This method is valid for ANY picture that you might send it as a parameter.  Try it out on the footballfield image.  What do you observe?

    > field = makePicture( pickAFile() )
    > show(field)
    > decreaseRed(field)
    > repaint(field)

[SIG1]  When you have the decreaseRed() method working, demonstrate this to a TA.


Activity B : Increasing the Red in a picture

Of course, once you have the ability to decrease the red it might be nice to be able to increase the red.  Recipe 9 on page 61 of your book offers one mechanism for doing this.  Type the code for recipe 9 into the program area of JES.  You DO NOT need not start with a new file, nor do you need to remove the decreaseRed() method from Activity A.  Instead, just give yourself one or two blank lines after decreaseRed() before you start typing the increaseRed() method.

When you are done, save this file again (still named lab04.py) and test the code.  You can either do this by starting over from scratch:

    > mascot = makePicture( pickAFile() )
    > show(mascot)
    > increaseRed(mascot)
    > repaint(mascot)

OR, if you still have mascot as a defined object in memory you can work with the one you have been using...

    > increaseRed(mascot)
    > repaint(mascot)

[Q11]  What do you observe?

Invoke this method a few more times, such as :

    
    > increaseRed(mascot)
    > increaseRed(mascot)
    > increaseRed(mascot)
    > repaint(mascot)

[Q12]  What do you observe?  You may want to open the picture with your picture tool and see what happens as you run this method multiple times. 

[Q13]  What conclusion can you make about the setRed() method that you use in your increaseRed() method?

[SIG2]  Demonstrate your increaseRed() method for the TA/Instructor.


Activity C : Combinations of manipulations

Section 3.4 of your textbook describes two of your author's attempts to create a method called makeSunset().  He presents the code for these attempts in Recipe 11 and Recipe 12.  Let's try out his code for ourselves.

To begin with, download this copy of a beach scene:

Add the makeSunset() method ( Recipe 11, page 62 in your book) into the file lab04.py in the definitions pane of JES.  ).

[Q14]  Code wise, what will happen to the pixels in a picture when you run this method?

Invoke this method by passing it a Picture object made from beach.jpg (as you did above)

[Q15]  What changes do you observe when you look at the resulting picture?

 

Next, add the three functions in recipe 12 into lab04.py.  [To avoid problems, let's rename makeSunset() in this recipe as makeSunsetAgain()]

[Q16]  Code wise, what will happen to the pixels in a picture when you run makeSunsetAgain() ? 

Invoke this method by passing it a Picture object made from beach.jpg (as you did above)

[Q17]  What changes do you observe when you look at the resulting picture?

[Q18]  What differences can you observe between these two ways to make a sunset?

[SIG3]  Demonstrate these methods for the TA/Instructor


Challenge/Bonus Activity : Manipulating colors to make other effects

The other day I was looking at a picture of my son:

(today.jpg)

playing with a toy I had as a kid:

(yesterday.jpg)

I was noticing that if you didn't know me or my son very well you might not really notice much difference between those two photos OTHER than the fact that the picture of me simply "looks" older.  That got me thinking - what kind of filter could I write to take a "modern" picture and make it look like an "old" color photo.

For this activity, write a method called ageIt() that takes a picture as a parameter (as all of the methods above did).  Use some combination of increasing/decreasing the red, green, and blue, components of each pixel to take the picture of my son and make it resemble the picture of me.  What you produce can take on any combination of settings of your choice, but should involve tweaks to all three color channels and be distinct from any of the methods above.

Suggestion : Use the color tool you used in Part A to look at the pixel color values for those things in both pictures (the green of the roof, the red of the barn, the skin on our hands??) and consider how the color channels changed between the two pictures.

[SIG4]  Demonstrate this method for the TA/Instructor