Академический Документы
Профессиональный Документы
Культура Документы
LECTURE TOPICS
Mid Semester Recess Period More Picture Techniques Sound and Arrays More Sound and Arrays Program Design and Strings Lists, Files and Modules Web, Representations, Steganography Turtles and Other Classes Revision and Look Ahead No formal classes - MUST be available normal & supplementary period
Assignment due 3:00 Tuesday May 21 Practical Test 2 in Lab class Practical Test 1 in Lab class
Lecture Topics and Lab topics are the same for each week
Revision
Last week we used getPixel() pix = getPixel(myPic, 7, 7)
0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
Revision
Last week we used getPixel() pix = getPixel(myPic, 32, 32) Remember . . . pix is a name that we made up The = symbol indicates an assignment statement, which takes the value on the right and assigns it to the name (variable) on its left
5
Arguments
When we call a JES function, we type its name and a pair of parentheses We might also need to type some values within the parentheses these are called arguments
Arguments
makePicture ( __ )
requires a single argument, a file name
Arguments
If we call a function with the wrong number or types of arguments, JES will tell us weve made an error The error message can be helpful, but you might have to work to understand it.
Every name refers to an item of a particular type pix on the Revision slide is an item of type pixel How do we know? Try >>> print pix
10
11
12
14
Colour channels
getRed() find the individual getGreen() channels of a pixel getBlue()
15
Colour channels
getRed() find the individual getGreen() channels of a pixel getBlue() setRed() setGreen() alter the individual setBlue() channels of a pixel
16
Colour channels
getRed() find the individual getGreen() channels of a pixel getBlue() setRed() setGreen() alter the individual setBlue() channels of a pixel >>> >>> >>> >>> setRed(pix, 0) print pix pixGreen = getGreen(pix) print pixGreen
17
Programs in JES
Until now weve entered commands in the command area (interaction area) of JES Now were going to write programs in the program area
program area
command area
18
Programs in JES
A program consists of one or more functions
def pickAndPlay(): myFile = pickAFile() mySound = makeSound(myFile) play(mySound)
19
Programs in JES
def pickAndPlay(): myFile = pickAFile() mySound = makeSound(myFile) play(mySound)
20
Programs in JES
def pickAndPlay(): myFile = pickAFile() mySound = makeSound(myFile) play(mySound)
21
Programs in JES
def pickAndPlay(): myFile = pickAFile() mySound = makeSound(myFile) play(mySound)
22
Programs in JES
def pickAndPlay(): myFile = pickAFile() mySound = makeSound(myFile) play(mySound)
23
Programs in JES
def pickAndPlay(): myFile = pickAFile() mySound = makeSound(myFile) play(mySound)
24
27
Then in the body of the function we use the name filename when we want to refer to the parameter
28
parameter
>>> showPicture(caterpillar.jpg) The arguments are expressions provided when the function is called.
29
>>> showPicture(caterpillar.jpg)
30
31
>>> makePicture(caterpillar.jpg)
32
33
A program
34
35
The programs written in the program area, but JES cant yet run it
36
37
38
39
Iteration
That was a lot of code just to turn five pixels yellow One of the most powerful aspects of programming is the ability to tell the program to repeat the same thing over and over, with minor variations This is called iteration Were going to see how to alter large numbers of pixels with less code than we used in yellowLine() But first we need to know about arrays
40
Arrays
An array is a collection of items all of which have the same name but each of which has a different index, an identifying number
Note: the first element has index 0, the second has index 1,
41
Arrays
An array is a collection of items all of which have the same name but each of which has a different index, an identifying number
height[0] height[3]
pix = getPixel(myPic, 3, 2)
pix
0 1 2 3 4 5 0 1 2 3 4 5
43
0 1 2 3 4 5 0 1 2 3 4 5
pixels
44
0 1 2 3 4 5 0 1 2 3 4 5
pixels
This is one you probably dont want to print, unless youre very patient (Though you could try it on a small picture, say 150x150)
45
Range
Range is an interesting function that gives us all the values in the range we specify (sort of) >>> print range(1, 10) >>> print range(1, 100)
46
Range
In fact, as you see, it gives us all the integer values from the first one (inclusive) to the last one (exclusive) That is, it gives us all the values from the first to one less than the last.
There is a good reason for this, but its tricky, so for now its easiest just to accept it
47
48
49
50
51
52
53
54
Note the increasing indentation . . . the body of the function must be indented from the def the body of the for statement must be indented from the for
56
Try not indenting the body and see if you can explain what seems to be a strange error message
57
58
59
61
Even show(myPic) has a strange quirk: it sometimes seems to show a previous version of the picture. So maybe we should just stick with repaint() unless we particularly want to use the features of explore()
62
63
64
65
66
An array is a collection
Weve used for to visit every number in a range But a range is only one kind of collection An array is another kind of collection.
67
An array is a collection
If we use for with an array of pixels, it will visit every pixel in the array pixels = getPixels(myPic) for pixel in pixels: <do something to pixel> This is what well now do as we manipulate pictures in various ways
68
Halving a colour
def halveRed(picture): pixels = getPixels(picture) for pixel in pixels: redVal = getRed(pixel) setRed(pixel, redVal / 2) This gives every pixel in the picture a new red value thats half the red value it had before (It doesnt alter the values in the green or blue channels)
halveRed
barbara.jpg
69
Doubling a colour
def doubleGreen(picture): pixels = getPixels(picture) for pixel in pixels: greenVal = getGreen(pixel) setGreen(pixel, greenVal * 2) This gives every pixel in the picture a new green value thats double the green value it had before (It doesnt alter the values in the red or blue channels)
doubleGreen
barbara.jpg
70
Doubling a colour
Remember a color has a maximum value of 255 200 * 2 = 400 ???
71
Doubling a colour
Remember a color has a maximum value of 255 400 modulo 255 = 145 200 * 2 = 400 ??? max 255 = 255
72
Doubling a colour
Remember a color has a maximum value of 255 400 modulo 255 = 145 200 * 2 = 400 ??? max 255 = 255 What really happens actually depends on how the options are set - either to wrap colours (modulo) or not see Edit/Options & modulo.
73
Adjusting a colour
def adjustBlue(picture, amount): pixels = getPixels(picture) for pixel in pixels: blueVal = getBlue(pixel) setBlue(pixel, blueVal * amount) This gives every pixel in the picture a new blue value thats amount times the blue value it had before It can be used to increase (amount > 1) or decrease (amount < 1) blue values, depending on the value of amount (and how the Modulo option is set)
74
75
pixel is an individual pixel; but each time through the loop it's a different pixel greenVal, redVal, and blueVal are integers
77
Negative
Some of you might be familiar with photographic negatives, although they dont exist in digital photography The negative has colours the exact opposite of the normal picture
78
Negative
In RGB terms, the opposite of a colour is 255 minus that colour in each of the three channels To make a negative, set the red to 255 minus what it was set the green to 255 minus what it was set the blue to 255 minus what it was >>> negRed = 255 currentRed >>> negGreen = 255 currentGreen >>> negBlue = 255 - currentBlue
79
Unlike the doubling, this one wont have colour values wrapping around. Why not?
80
Greyscale
In RGB terms, black, grey, and white all have one thing in common equal red, green, and blue values
82
Greyscale
To turn a colour picture into a greyscale picture, we need to set red, green, and blue to the average of their original values (add up red, green, blue and divide by 3)
greyVal = (getRed(pixel) + getGreen(pixel) + getBlue(pixel))/3 setRed(pixel, greyVal) setGreen(pixel, greyVal) setBlue(pixel, greyVal)
All this, of course, in a loop in a function so that each pixel in the image is changed.
83