Introduction to Multimedia Programming



Introduction to Multimedia Programming

Mini-Lab 2: Running a program in JES

The objective of this mini-lab is to become familiar with how to run a program in JES, as well as how to make a program more general, and then how to write a picture to the hard drive.

Part 1: Running a program in JES

1. Start up JES. In your text, the first program you saw was to pick and show a picture. (See Recipe 1 on page 29 of your text). Type this program into the program area of JES.

2. Now you must save and load the file. Click the Load button. A dialogue box will ask you if you would like to save. Click OK, then save the file with a name that ends in “.py” to indicate a Python file. You could use pickAndShow.py. The file will then automatically load.

3. To run the program, type pickAndShow() in the command area and hit enter. What happened? Was it what you expected?

4. Let’s try running another, slightly more interesting, program. Copy and paste the Jython code below into a new JES file.

# Simple Picture Example

def example1():

myFile = 'waikiki.jpg'

myPict = makePicture(myFile)

show(myPict)

for pixel in getPixels(myPict):

redValue=getRed(pixel)

newColor = makeColor(redValue, 0, 255-redValue)

setColor(pixel, newColor)

repaint(myPict)

# Add example2() here:

5. Save and load the file. Click the Load button. A dialogue box will ask you if you would like to save. Click OK, then save the file as minilab2.py. The file will then automatically load.

6. To run the program, type example1() in the command area and hit enter. What happened? Did you get an error message? Why didn’t it run?

7. To make this program work, we need to know the entire path name of the file waikiki.jpg. The path name of a file is like its address in the computer. Each part of the path name gives the computer a smaller, more defined place to look for that file. Every drive, directory, and folder must be noted in the correct order and format for the computer to locate the file. Replace ‘waikiki.jpg’ with the full path and filename. If you are unsure how to do this, here’s a trick:

• In the command area of JES type this:

pickAFile()

… and then press Enter. That will bring up a window that will let you

browse the folders on your computer (it should start out by showing you

the contents of the JES folder). Find where you placed MediaSources

and then locate waikiki.jpg. Select it and press open. If all went well,

you’ll notice that the contents of the bottom portion of JES has changed.

It now shows the full path of the file you selected surrounded by single-

quotes. Copy and paste this to replace the ‘waikiki.jpg’ in the

code.

8. Since we have made a change to our minilab2.py file, we need to load it into JES before trying to run example1() again. So click on the Load button, and then reply OK to the prompt that pops up.

9. Type example1() in the command area again. You should first see a picture of a beach, and then it should be replaced by a red and blue picture of that same beach.

Part 2: Making a Program More General and Writing a Picture to the Hard Drive

What if we wanted to show a different .jpg file? We would have to change the function to include the entire path name of the new file and then load the new function. We would have to do this every time we wanted to show a new picture. How annoying! Fortunately, there is an easier way to do this. Remember that pickAFile() trick we saw earlier? The function called pickAFile()will let you find the full path to a file on your computer. You can incorporate it into your function. Here’s how:

1. Copy and paste example1() into the space below the line that says

# Add example2() here:

2. Change the name of the function to example2().

3. Then replace '..\\JES\\MediaSources\\waikiki.jpg' in your new program with pickAFile().

4. Since we have made a change to our minilab2.py file, we need to load again to save the changes to the file.

There is one more change we want to make. What if we want to see our picture after we quit JES or share our picture with someone else? JES has a way to do this as well. We use the command writePictureTo(). To do this, we will add one more line of code to our program. At the very end of the function and at the same indentation as the repaint(myPict) command we want to add the command writePictureTo().

The command writePictureTo() uses two things. First, the picture we want to write out. Second, the location we want to write it to (let’s put it in the MediaSources folder).

5. At the end of the function, add the writeToPicture() command. It should look something like:

writePictureTo(myPict, 'C:\\JES\\MediaSources\\MyNewPicture.jpg')

It is VERY important not to forget the .jpg at the end. Also, remember that if your MediaSources folder is in a different place on your computer, then the path in the writePictureTo function will be different from what is shown here.

6. Next, type example2() in the command area and hit enter. A file browser window will appear, allowing you to choose which file you would like to show. Go ahead and pick a different landscape or beach JPEG picture on your computer. Try this with several different pictures.

Once again, you will first see the original picture, then the modified picture. As an added benefit you will now have your picture stored on your computer at the location you specified in the writePictureTo command.

7. We can implement one more change to make our function more general. We just saw that if we wanted to show different files, we could use the pickAFile() command right inside the function. Another way to do this is to add an input variable, or parameter, to the function. Change the first line (the def line) of example2 to include myFile as a parameter. It should then look like:

def example2(myFile):

8. We now no longer need to have the line “myFile = …” in the function, so remove it.

9. Now, when we execute the program, we will need to give the specific filename as input. So, when we type something like

example2(“C:\\JES\\MediaSources\\waikiki.jpg”)

in the command area and hit enter, the variable myFile will take on the value

“C:\\JES\\MediaSources\\waikiki.jpg”.

Alternatively, you may also execute the program by typing

example2(pickAFile())

and then the variable myFile will take on the value of whatever file you choose.

10. Finally, create a web page for this minilab which should contain the original picture, along with the red/blue version of the picture. Include some text to describe what you did to the original picture. Make a link to this new page from your course web page.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download