Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
LECTURE TOPICS
Programs, Arrays and Iteration Mar 11 This week in the lab there will be a Working with x andcan y coordinates Mar 18 practical practice so you practice for Selection Mar5s 25 exam. week Apr 1 - Apr 7 Mid Semester Recess Period
Practical Test 1 in Lab class
ButApr dont forget you still need to do the tut 8 More Picture Techniques material week Apr 15 for this Sound and Arrays
Apr 22 Apr 29 May 6 May 13 May 20 May 27 Jun 3 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
Lecture Topics and Lab topics are the same for each week
myFile newFile
6
Revision - Functions
Programs are made up of one of more functions If we want a function to take arguments, we include corresponding parameters when defining it Whenever we find the same piece of code appearing several times in a program, we should extract it, define it as a function, and replace the multiple occurrences with multiple calls to the function If there are variations between the occurrences, we use parameters and arguments to deal with the variations
7
sequence
sequence
loop
Revision - Comments
Your programs must include three kinds of comments:
1.A comment at the start of every program (collection of functions), saying who wrote it, and when, and why 2.A comment at the start of every function, explaining briefly what it does 3.A comment with every bit of code that another programmer might find easier to understand if its explained
selection loop
sequence
11
12
The if statement
Selection is done with the if statement. At its simplest, the statement takes the form if <condition> <body>
?
selection
13
The if statement
Selection is done with the if statement. At its simplest, the statement takes the form if <condition> <body>
?
selection
where <condition> means some condition to be tested and <body> is one or more statements suitably indented
14
The if statement
If the condition is true when tested, the body will be executed; otherwise, the body will be ignored if <condition> <body>
?
selection loop
Conditions
A condition is something that, when evaluated, gives a value of true or false if <condition> <body> Technically, it is a boolean expression, which simply means an expression whose value is true or false boolean is another simple type to add to int, float, and string
16
Conditions
Unfortunately, Python implements booleans as integers, which can lead to some confusion: The int 0 is regarded as the boolean false any other int is regarded as the boolean true false true 0 any other int (e.g. 1,6,10)
17
Boolean expressions
We can make simple boolean expressions by comparing numbers with relational operators
18
Boolean expressions
We can make simple boolean expressions by comparing numbers with relational operators Note that the check for equality uses a double = sign - because the single one is reserved for assignment
19
Boolean expressions
We can make simple boolean expressions by comparing numbers with relational operators Note that the check for equality uses a double = sign - because the single one is reserved for assignment With the other 2-character operators, the order of the characters is important
20
This is a function that measures how close together two colours are It goes up to about 450 Experiment with different colours to see what sorts of values it produces
21
22
For all the pixels in the picture, if the colour of that pixel is close to white enhance the green of a pixel
23
Irradiating a swan
This method works well with swan.jpg def irradiate(picture): # Take pixels that are near enough to # white and make them very green for px in getPixels(picture): colour = getColor(px) if distance(colour, white) < 270: # Found distance by trial & error setBlue(px, getBlue(px) / 2) setRed(px, getRed(px) / 2) setGreen(px, 190) repaint(picture)
24
Irradiating a swan
25
Irradiating a swan
For all the pixels in the picture, if the colour of that pixel is close to white enhance the green of a pixel
26
Red-eye reduction
To reduce the red that a camera flash puts in the eyes, without reducing the red in the rest of the picture, we need to first note the range of pixels in which the eyes are located.
28
Red-eye reduction
To reduce the red that a camera flash puts in the eyes, without reducing the red in the rest of the picture, we need to first note the range of pixels in which the eyes are located. Instead of reducing the red in the whole picture - we reduce it just in the range with the eyes in it
29
Red-eye reduction
for x in range(startX, endX): for y in range(startY, endY): pix = getPixel(pic, x, y) if distance(red, getColor(pix) < 165): setColor(pix, replacementColour)
startX endX
startY endY
30
Red-eye reduction
for x in range(startX, endX): for y in range(startY, endY): pix = getPixel(pic, x, y) if distance(red, getColor(pix) < 165): setColor(pix, replacementColour)
startX endX usually a blackish colour startY endY
31
if with else
Weve seen the simplest form of the if statement if <condition>: true <body>
?
32
if with else
What if we want to do something when the condition is true and something different if the condition is false? For example, we want to turn the white pixels green and the other pixels purple?
?
true false
33
if with else
What if we want to do something when the condition is true and something different if the condition is false? For example, we want to turn the white pixels green and the other pixels purple?
?
true false
34
if with else
When we follow an if and its body with an else and its body.. the body of the if will be done if the condition is true, and the body of the else will be done if the condition is false
if distance(colour, white) < 270: <make the pixel more green> else: <make the pixel more purple>
?
true false
35
if with else
Guzdial & Ericson prefer to achieve the same effect with two separate ifs if distance(colour, white) < 270: <make the pixel more green> if distance(colour, white >= 270: <make the pixel more purple>
?
true
?
true
36
if with else
if distance(colour, white) < 270: <make the pixel more green> else # distance(colour, white >= 270: <make the pixel more purple> But most programmers prefer the proper use of else
?
true false
37
elif is short for else if; be sure that you understand the else bit
39
Posterising
A posterised picture is one that uses a highly reduced set of colours (suitable for poster printing)
40
Posterising
if-elif-else statements form the basis of a function that can be used to posterise a picture It divides the possible values into four ranges, then replaces every value with the midpoint of that range
0-63 31
64-127 95
128-191 159
192-255 223
41
Posterising
The posterise function is typically used for each colour channel (red, blue, green)
def posterise(picfile): # Posterise a picture by replacing each channel of every pixel # with the middle of the range of values it lies in picture = makePicture(picfile) for px in getPixels(picture): setRed(px, midRange(getRed(px))) setGreen(px, midRange(getGreen(px))) setBlue(px, midRange(getBlue(px))) repaint(picture)
42
Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value
0-63 31
64-127 95
128-191 159
192-255 223
43
Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value
0-63 31
64-127 95
128-191 159
192-255 223
44
Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value
0-63 31
64-127 95
128-191 159
192-255 223
45
Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value
0-63 31
64-127 95
128-191 159
192-255 223
46
Posterising
def midRange(value): # See which of four ranges a colour channel value lies in, # and return the midpoint of that range if value < 64: value = 31 elif value < 128: value = 95 elif value < 192: value = 159 else: value = 223 return value
0-63 31
64-127 95
128-191 159
192-255 223
47
Posterising
Our approach (using elif) is basically the same as the version in the textbook, but our program is a lot smaller and neater Lecture4demo.py shows this posterise function and the one from the book
48
49
50
51
repaint(picture)
repaint(picture) return(picture)
52
53
Combining conditions
Sometimes a simple condition isnt enough We might want to combine multiple conditions in various ways We can do this with the logical operators and, or, and not
54
Combining conditions
and, or, and not mean pretty much what they mean in English if age > 16 and < 25
55
Combining conditions
and, or, and not mean pretty much what they mean in English if age > 16 and < 25
An important difference is that they must join complete conditions, not partial ones if age > 16 and age < 25
56
Edge detection
We can turn a colour picture into a black-and-white line drawing if we can find edges and make them black An edge is where theres a sharp difference in luminance (brightness) between neighbouring pixels
57
Edge detection
Carefully read and understand the edge detection program in the book Carefully read and understand the edge detection program in lecture4demo.py Try to understand the differences; for example, theres no need to have two copies of the picture, as in the book And the proper use of else means we dont need to use not
58