Title: CS1315: Introduction to Media Computation
1CS1315Introduction to Media Computation
- Programming picture manipulations
2Administrivia
- Grades will be available via WebWork
- ltInsert Demo of WebWork heregt
- Does everyone have JES installed now?
3Demonstrating Manipulating Colors
gtgtgt print color color r81 g63 b51 gtgtgt print
newcolor color r255 g51 b51 gtgtgt print
distance(color,newcolor) 174.41330224498358 gtgtgt
print color color r168 g131 b105 gtgtgt print
makeDarker(color) color r117 g91 b73 gtgtgt print
color color r117 g91 b73 gtgtgt
newcolorpickAColor() gtgtgt print newcolor color
r255 g51 b51
gtgtgt print getRed(pixel) 168 gtgtgt
setRed(pixel,255) gtgtgt print getRed(pixel) 255 gtgtgt
colorgetColor(pixel) gtgtgt print color color r255
g131 b105 gtgtgt setColor(pixel,color) gtgtgt
newColormakeColor(0,100,0) gtgtgt print
newColor color r0 g100 b0 gtgtgt
setColor(pixel,newColor) gtgtgt print
getColor(pixel) color r0 g100 b0
4We can change pixels directly
gtgtgt file"/Users/guzdial/mediasources/barbara.jpg"
gtgtgt pictmakePicture(file) gtgtgt show(pict) gtgtgt
setColor(getPixel(pict,10,100),yellow) gtgtgt
setColor(getPixel(pict,11,100),yellow) gtgtgt
setColor(getPixel(pict,12,100),yellow) gtgtgt
setColor(getPixel(pict,13,100),yellow) gtgtgt
repaint(pict)
But thats really tedious Manipulating pictures
more cleverly is the next topic
5How do you find out what RGB values you have? And
where?
(especially useful when testing and debugging)
6Use a loop!Our first picture recipe
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
Used like this gtgtgt file"/Users/guzdial/mediasour
ces/katie.jpg" gtgtgt picturemakePicture(file) gtgtgt
show(picture) gtgtgt decreaseRed(picture) gtgtgt
repaint(picture)
7How loops are written
- for is the name of the command
- An index variable is used to represent the
different values in the loop - The word in
- A function that generates a sequence
- The index variable will be the name for each
value in the sequence, each time through the loop - A colon ()
- And, another block
8What happens when a loop is executed
- The index variable is set to the next (or first,
at the beginning) item in the sequence - The block is executed
- The variable is often used inside the block
- Then execution returns to the for statement,
where the index variable gets set to the next
item in the sequence - Repeat until the sequence is exhausted.
9getPixels returns a sequence of pixels
- Each pixel knows its color and its original
picture - Change the pixel, you change the picture
- So the loop below assigns the index variable p to
each pixel in the picture picture, one at a time.
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
10Do we need value?
- Not really Remember that we can swap names for
data or functions that are equivalent.
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
def decreaseRed(picture) for p in
getPixels(picture) setRed(p, getRed(p) 0.5)
11Lets walk that through slowly
Here we get a picture object in as input and call
it picture
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
picture
12Now, get the pixels
We get all the pixels from the picture, then make
p be the name of each one one at a time
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
picture
getPixels()
Pixel, color r135 g116b48
Pixel, color r133g114 b46
Pixel, color r134 g114b45
p
13Get the red value from pixel
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
We get the red value of pixel p and name it value
picture
getPixels()
Pixel, color r135 g116b48
Pixel, color r133g114 b46
Pixel, color r134 g114b45
value 135
p
14Now change the pixel
Set the red value of pixel p to 0.5 (50) of value
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
picture
getPixels()
Pixel, color r67 g131 b105
Pixel, color r133g114 b46
Pixel, color r134 g114b45
value 135
p
15Then move on to the next pixel
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
Move on to the next pixel and name it p
picture
getPixels()
Pixel, color r67 g131 b105
Pixel, color r133g114 b46
Pixel, color r134 g114b45
value 135
p
16Get its red value
Get its red value
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
Change value to the new red value at the new p
picture
getPixels()
Pixel, color r67 g131 b105
Pixel, color r133g114 b46
Pixel, color r134 g114b45
value 133
p
p
p
17And change this red value
def decreaseRed(picture) for p in
getPixels(picture) valuegetRed(p)
setRed(p,value0.5)
Change the red value at pixel p to 50 of value
picture
getPixels()
Pixel, color r67 g131 b105
Pixel, color r66g114 b46
Pixel, color r134 g114b45
value 133
p
p
p
18And eventually, we do all pixels
19Tracing/Stepping/Walking through the program
- What we just did is called stepping or walking
through the program - You consider each step of the program, in the
order that the computer would execute it - You consider what would specifically happen there
- You write down what values each variable (name)
has at each point. - Its one of the most important debugging skills
you can have. - And everyone has to do a lot of debugging,
especially at first.
20Did that really work?How can we be sure?
- Sure, the picture looks different, but did we
actually decrease the amount of red? By as much
as we thought? - Lets check it!
21gtgtgt file pickAFile() gtgtgt print
file C\Documents and Settings\Mark Guzdial\My
Documents\mediasources\barbara.jpg gtgtgt pict
makePicture(file) gtgtgt pixel getPixel(pict,1,1) gt
gtgt print pixel Pixel, colorcolor r168 g131
b105 gtgtgt decreaseRed(pict) gtgtgt newPixel
getPixel(pict,1,1) gtgtgt print newPixel Pixel,
colorcolor r84 g131 b105 gtgtgt print 168
0.5 84.0
22Checking it in the MediaTools
23If you make something you like
- writePictureTo(picture,filename)
- Writes the picture out as a JPEG
- Be sure to end your filename as .jpg!
- If you dont specify a full path,will be saved
in the same directory as JES.