Computer Science 111 - Spring Semester, 2008 -
Homework #5
due: by midnight on Thursday March 13
Part 1. Setting up the image and sound files Your task in this week's
programming homework is to use definite loops (for loops) to
manipulate images and sounds using jython and the jes
media.py module.
Make sure to boot into Linux!
If you choose your own sound files as described below, consider using
this source:
http://www.blueharvest.net/sound/.
There are two different kinds of wav files and this one definitely has
the right kind for JES.
We can manipulate jpeg images (files end with .jpg) and wav files (.wav)
using the tools in media.py.
You will need 4 images and 4 very short sound files.
You are welcome to use my image and sound files. My theme is a
Star Wars theme. You can also choose your own theme and
find files on the web. The sound files must be in
a subdirectory called sounds (in the Desktop/jes/ directory).
There is already a subdirectoy called images. You must place
your images there.
If you want to use mine, run the Linux commands
below that change directories to
Desktop/jes, make a sounds directory, change to the sounds directory,
get all the sound (.wav) files, change back up one level, change
to the images directory, and get all the images. Then change
up one directory level (back to Desktop/jes).
cd Desktop/jes
mkdir sounds
cd sounds
getcopy sounds/*.wav
cd ../
cd images
getcopy images/*.jpg
cd ../
Type ls sounds then ls images
to see all the files you have now!
Part 2. A picture show.
After you open a file called hw5.py using emacs, enter
the information needed at the top to document it.
Then, since we are using the media.py module as well as
the sleep function from the time module, place these
two python statements at the top of your file:
from media import *
from time import sleep
We have found that JES is sometimes getting confused about where files are.
So, we will use the full pathname for filenames.
Making sure you are in the Desktop/jes directory,
at the beowulf prompt, type
pwd
This is the full pathname starting from the top or root directory of the whole system.
You'll get something like
/home/classes/111b/Desktop/jes
Highlight this, and right click with the mouse. Choose copy.
Then in emacs, at the start of your definition of the main function,
type
path = "
then right-click and paste. Then type
/"
to end the directory (with "/") and to end the string (with the double quote).
Use an assignment statement to define a variable called f1, and
set it equal to:
f1 = path+"images/starWars1.jpg"
The value stored in f1 will actually be
"/home/classes/111b/Desktop/jes/"+ "images/starWars1.jpg" which is simply
"/home/classes/111b/Desktop/jesimages/starWars1.jpg"
Yours will be somewhat different, and will contain your username in the path
instead of 111b.
Now assign to variable p1:
p1 = makePicture(f1)
Now call the function show(), passing it p1:
show(p1)
Save your file and call JES as you did in lab:
[111b@localhost jes]$ sh JES.sh
Jython 2.1 on java1.5.0_07 (JIT: null)
Type "copyright", "credits" or "license" for more information.
>>>
import hw5 and then call hw5.main(). Make sure this part works before moving on.
Important Note: Jython keeps around old information about
a file when you import it. If hw5.py has errors and you fix them
in emacs, make sure you EXIT jython by typing ctrl-d and ctrl-c
and then rerun it by typing
[111a@localhost jes]$ sh JES.sh
before importing hw5 again. Otherwise the changes you made in emacs are not imported.
Part 3. Add more pictures: Now create
3 more variables, f2, f3, and f4 that have strings of jpg files
stored in them. Use the path variable as you did with f1.
And make 3 more variables p2, p3, p4 that
are assigned values using the makePicture() function.
Then show each one by calling show() and passing it the
correct p-variable. In between each call to show(), call the
function sleep(). Sleep needs to be passed how many seconds you
want the program to stall. e.g. show(2) makes it stall for 2
seconds before showing the next picture. You decide
you how long to have it sleep. It can be passed floating point
numbers as well such as 1.5 for 1 and one-half seconds.
Save and exit emacs, run jes, import hw5 and call hw5.main().
Make sure it works properly before moving on to the next part.
Remember the above note!
Part 4. Add sounds:
Your .wav files must be stored in the sounds sub-directory of jes.
Please make sure to use wav files that are really short.
No more than a few seconds each.
You can reuse the variable path:
path = "/home/classes/111b/Desktop/jes"
Set up variable sf1 to store the first sound file name:
sf1 = path + "sounds/yodalaff.wav"
Do this for four sound files, using variables
sf1, sf2, sf3, sf4.
We're going to use a for loop to call makeSound() on
each of these files and we'll store the results in a list!
First set the variable sounds equal to the empty list
sounds = []
Construct a for loop that has as a sequence a
list that contains variables sf1, sf2, sf3, and sf4.
Make your loop variable i.
In the body of the for loop, you'll have just one statement.
- In the statement you'll call
the list append() function.
The second statement is
sounds.append(makeSound(i))
Notice that the index variable is passed into the
function makeSound().
Now construct another for loop that goes through the
list stored in variable sounds, and
has two statements in its body.
- The first one is a call to the function
play(), and it must be passed each sound to play.
You have to figure that out, but remember that the
list/sequence used in the for loop is the one
you just made, stored in the variable sounds.
- The second statement calls the sleep() function. You decide
how many seconds to pause between sounds.
Make sure this works before moving on to the next part.
Remember that you may need to adjust the volume using
kMix as you did in lab.
Part 5. Add lots of red to an image (we also did this in lab):
The function getPixels() makes a list of all the
picture elements (pixels) in an image. Very handy!
Call the function getPixels() and make sure to pass it
the image stored in variable p4. Assign its return value to
a variable called pixels. This will be a sequence of pixels we
can use in a for loop.
So make a for loop that uses the variable pixels
as its sequence. The for loop will have one statement in its
body:
- The statement is a call to function setRed(). setRed()
must be passed two values: one pixel, and one value between 0 and 255.
255 is the highest red value possible. So make the
second value passed into setRed() be 255.
After the loop, call repaint(p4) to see the changed pixels.
Make sure this all works before moving to the next part.
Part 6. Sounding off!
Choose your favorite sound. Play it 30 times, with
a 0.1 second delay between each time. Notice that
if the sound takes longer that 0.1 seconds to play, the
sound will overlap itself. That's fine. Of course, use a for loop!
Part 7. do some more image manipulation
In the same file, after the definition for main(),
write another definition for a function called checker(). It will start
like this (and you can copy and paste from the web page if you want to:
def checker():
path = "/home/classes/111b/Desktop/jes/"
file = path + "images/starWars4.jpg"
picture = makePicture(file)
show(picture)
x = getWidth(picture)
y = getHeight(picture)
x_third = x/3 # one-third of the way across
y_third = y/3 # one-third of the way down
# i is the
for i in range(1,x+1):
for j in range(1, y+1):
pixel = getPixel(picture, i, j)
Still inside the inner for-loop, after getting the pixel,
use an if-else statement to check the values of i, and j.
Use setGreen() and setRed() to set either the red or the green value of pixel to
255 depending on the i,j values. Make a checkerboard like this:
I used one if-else statement, with another if-else statement inside each
part. You may do this, or construct a long if-elif-...-elif-else statement.
There are a number of ways to do it.
Don't forget that you have the values x_third and y_third to work with.
To run this function just do what you usually do:
jes
>>> import hw5
>>> hw5.checker()
Part 8. Submitting
Since these files are all in your Desktop/jes/ subdirectory, make
sure you are in that directory when submitting your homework files.
Please make sure you do not call main() at the end of your program.
Also make sure to include the filename, description of the program, your name, account number, and
date at the top, in comments.
Name the program hw5.py. Make sure
you are in directory Desktop/jes, and submit hw5.py by the due date with the command
submit homework5 hw5.py
You must also submit all of your .jpg and .wav files, even if you used
some of mine.
cd images
submit homework5 *.jpg
cd ../sounds
submit homework5 *.wav
Part 9. Pair programming
Make sure that your partner has all of the image and sound files
and a copy of hw5.py. If you used my images and sounds,
the partner can use getcopy to get them. Then just use
sftp as we did in the first lab to transfer a copy
of hw5.py the the partner. sftp can also transfer image and
sound files.