Académique Documents
Professionnel Documents
Culture Documents
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
Mid Semester Recess Period More Picture Techniques Sound and Arrays More Sound and Arrays
Practical Test 1 in Lab class
29 Program Design and lab Strings you are You Apr must attend the Week 9 May 6 Lists, Files and Modules registered in or you will not be Week 10 May 13 Web, Representations, Steganography ableMay to do the practical test Week 11 20 Turtles and Other Classes Week 12 Week 13 May 27 Jun 3 Revision and Look Ahead No formal classes
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
selection loop
sequence
10
11
12
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
Oversight makeEmptyPicture()
Earlier lectures overlooked something thats pretty important in image manipulation Weve always manipulated an image itself, changing the original Sometimes one might want to keep the original and work with a copy
14
Oversight makeEmptyPicture()
Once you have the height and width of a picture, makeEmptyPicture(width, height) can give you a new picture of the required size You can then copy (the colours of) the pixels from the original to the copy, and then manipulate the copy without altering the original
15
Oversight makeEmptyPicture()
width height
makeNewPicture(width, height)
16
Oversight makeEmptyPicture()
So you can use this to: 1) create a new picture and 2) copy (the colours of) the pixels from the original to the copy 3) manipulate the copy without altering the original
17
18
Blending images
To get the effect of transparency, looking through one somewhat transparent image at another image, all we have to do is average the colours of the corresponding pixels.
19
Blending images
To overlay picture 1 and picture 2, we change the pixels of picture 1 to be the averages of the pixels of the two pictures If we do a 50/50 average we get half of each picture; different proportions give different apparent transparency
20
Blending images
To overlay picture 1 and picture 2, we change the pixels of picture 1 to be the averages of the pixels of the two pictures If we do a 50/50 average we get half of each picture; different proportions give different apparent transparency
newPic1Pixel = (oldPic1Pixel*0.5) + (pic2Pixel*0.5)
newPic1Pixel
0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
oldPic1Pixel
0 1 2 3 4 5 0 1 2 3 4 5
pic2Pixel
0 1 2 3 4 5
21
Transparent overlay
Lets overlay a picture of a butterfly on a picture of a bridge
480 422
640
497
butterfly1.jpg bridge.jpg
22
Transparent overlay
We need to specify at which coordinates of picture 1 (bridge) to start overlaying picture 2 (butterfly) We need to specify how transparent to make picture 2
xstart ystart 50% transparent
23
Conditions
70% transparent
40% transparent
24
Conditions
If we want picture 2 to be 70% transparent, we blend the colour channels to be 70% of the pixel from picture 1 and 30% from picture 2
70% picture 1
30%
Conditions
picture 1
If we want picture 2 to be 40% transparent, we blend the colour channels to be 40% of the pixel from picture 1 and 60% from picture 2
40%
60%
(oldPic1Pixel*0.4) + (pic2Pixel*0.6) 26
Conditions
In general, if we want picture 2 to be n% transparent, we blend the colour channels to be n% of the pixel from picture 1 and (100 n)% from picture 2
picture 1
n%
(100 - n)%
n% transparent picture 2
(oldPic1Pixel*(n/100)) + (pic2Pixel*(100-n)/100)
27
Example
e.g. if we want picture 2 to be 80% transparent, we blend the colour channels to be 80% of the pixel from picture 1 and (100 80)% from picture 2
picture 1
n%
(100 - 80)%
Example
See transparency() in lecture5demo.py
def transparency(pic1, pic2, xstart, ystart, proportion): # Overlay pic2 on pic1, starting at coordinates xstart and ystart of pic1, # with proportion determining the transparency (0-100) of the overlay for x in range(0, getWidth(pic2)): for y in range(0, getHeight(pic2)): # x and y are coordinates in pic2; add xstart and ystart to get the matching coords in pic1 pix1 = getPixel(pic1, xstart + x, ystart + y) pix2 = getPixel(pic2, x, y) # Blend the channels: proportion% of pic1 and (100-proportion)% of pic2 newRed = getRed(pix1) * proportion / 100 + getRed(pix2) * (100 - proportion) / 100 newGreen = getGreen(pix1) * proportion / 100 + getGreen(pix2) * (100 - proportion) / 100 newBlue = getBlue(pix1) * proportion / 100 + getBlue(pix2) * (100 - proportion) / 100 newColour = makeColor(newRed, newGreen, newBlue) # And apply this new blended colour to the pixel of pic1 setColor(pix1, newColour) repaint(pic1) 29
Fading
The book blends one picture into another by doing a section of picture 1 alone, a section of 50/50 transparency, and a section of picture 2 alone This is worth examining to see how they manage the coordinates of picture 1, picture 2, and the new picture (They paint to a new picture, whereas were altering picture 1)
30
Fading
0%
picture 2
transparency
100%
But were going to do something quite different: A full fade, from 0 transparency at the top of picture 2 to 100% transparency at the bottom
31
Fading
0% 0
transparency
y coordinate
100%
getHeight(picture2)
Fading
0% 0
transparency
y coordinate
100%
getHeight(picture2)
How do the transparency levels relate to the coordinates? proportion = 100 * y / getHeight(pic)
33
34
Subtracting Background
When we overlaid the picture of the butterfly on the bridge, the picture of the butterfly was a rectangle that included background
35
Subtracting Background
When we overlaid the picture of the butterfly on the bridge, the picture of the butterfly was a rectangle that included background It might be good to get rid of the background, and only overlay the butterfly itself (the foreground) That is, we want to subtract the background from the picture To do this, we need to know which pixels are background and which are foreground
36
Subtracting Background
If we have one picture of the background alone, and one of the background with foreground . . . We can compare the two, and choose only the pixels that are different
background
background + foreground
37
Subtracting Background
That way, we get just the foreground pixels.
background + foreground
foreground
38
Subtracting Background
Which we can overlay on a third picture, (a new background)
foreground
39
foreground + background
background
40
41
42
Chromakey
Chromakey is a technique for subtracting background that relies on the background being as close as possible to a single colour . . . And a colour that doesnt occur very often in the foregrounds that we tend to work with
Chromakey
Because theyre filmed against a blue background, and blue is then eliminated from the picture in a background subtraction
(green is also used a lot neither blue or green occur in skin colours but watch what you wear)
44
Chromakey
If the foreground is photographed against a blue background . . . The function becomes simpler, because its always comparing with the same colour . . . And the result is a lot cleaner
45
Drawing on pictures
Like most programming languages, JES lets us add certain features directly to a picture
addLine(pic, x1, x2, y1, y2) addRect(pic, x1, y1, w, h) addRectFilled(pic, x1, y1, w, h) addOval(pic, x1, y1, w, h) addOvalFilled(pic, x1, y1, w, h)
a line from (x1, y1) to (x2, y2) a rectangle of width w and height h starting at (x1, y1) a filled rectangle an oval that fits in a rectangle of width w and height h starting at (x1, y1) a filled oval
Drawing on pictures
47
Writing on pictures
addText(pic, x, y, string)
the string (of text) starting at location (x, y)
This, too, can take an additional argument, colour Does the top of the string or the bottom of the string line up with the vertical coordinate y? How would you find out? To alter the font or size see Ch16 page 398 and look at the JES Pictures function addTextWithStyle()
48
Writing on pictures
49
50