410 likes | 514 Vues
CS1315: Introduction to Media Computation. Programming picture manipulations Goofing around with pictures. Administrivia. Grades will be available via WebWork Demo Lab 2 due this Friday Lab 3 due date is soon! Monday, August 30 th Does everyone have JES installed now?.
E N D
CS1315:Introduction to Media Computation Programming picture manipulations Goofing around with pictures
Administrivia • Grades will be available via WebWork • Demo • Lab 2 due this Friday • Lab 3 due date is soon! • Monday, August 30th • Does everyone have JES installed now?
New Jython and JES Functionsfor this Breakout… (Bingo?) • getPixel, setPixel • getRed/Blue/Green setRed/Blue/Green • getColor, setColor • makeColor • pickAColor • makeDarker, makeLighter • distance • for
Exploring Pictures • >>> aPic = makePicture(pickAFile()) • MediaTools -> Picture Tool… • Choose picture to investigate • Click and drag to get pixel values, positions • Select “Update Picture… -> Update” after you make changes to the picture • What are RGB values for a red pixel in the picture? • For a yellow pixel? • For orange?
Getting at Pixels in Code • >>> samplePixel = getPixel(aPic, 100, 100) • >>> print samplePixelPixel, color=color r=235 g=126 b=5 • What can we do with a pixel once we get it?
Pixel Colors • >>> print getColor(samplePixel)color r=235 g=126 b=5 • >>> print getRed(samplePixel)235 • >>> print getGreen(samplePixel)126 • >>> print getBlue(samplePixel)5
How Do We Set the Color? • We get the pixel colors by calling getRed(…) getGreen(…) or getBlue(…) • How do we set the colors?
setRed(), setGreen(), setBlue() • >>> setRed(samplePixel, 0) • >>> setGreen(samplePixel, 0) • >>> setBlue(samplePixel, 255) • Choose “Update” in Media Tools to see differences in Media Tools window • Update pictures shown with “show” with “repaint” • >>> repaint(aPic)
More Colors • >>> thisColor = makeColor(0, 0, 255) • >>> samplePixel = getPixel(aPic, 100, 101) • >>> setColor(samplePixel, thisColor) • Choose “Update” in Media Tools to see diff • >>> print getColor(samplePixel) • >>> newColor = pickAColor() • >>> setColor(samplePixel, newColor) • Choose “Update” in Media Tools to see diff
Making Colors Darker or Lighter • >>> aColor = pickAColor()java.awt.Color[r=102,g=102,b=255] • >>> print aColorcolor r=102 g=102 b=255 • >>> makeDarker(aColor) • >>> print aColorcolor r=71 g=71 b=178 • >>> makeLighter(aColor)color r=101 g=101 b=254
Predefined Colors • JES has predefined colors that you can use • blue, yellow, red, green, white… • >>> setColor(samplePixel, blue) • Don’t forget to hit update on Media Tools, or “repaint” for pictures shown with show()
Distance Between Colors • >>> firstColor = pickAColor() • >>> secondColor = pickAColor() • >>> distance(firstColor, secondColor)265.00377355803823 • “distance” is a measure of similarity between two colors • The more similar, the smaller the number • We’ll use this function more at a later date…
Reducing the Tedium • Drawing a blue line… • >>> setColor(getPixel(aPic, 100, 100), blue) • >>> setColor(getPixel(aPic, 101, 100), blue) • >>> setColor(getPixel(aPic, 102, 100), blue) • Works, but is tedious…
Use a loop!Our first picture recipe def decreaseRed(picture): for p ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) Used like this: >>> file="/Users/guzdial/mediasources/katie.jpg" >>> picture=makePicture(file) >>> show(picture) >>> decreaseRed(picture) >>> repaint(picture)
How loops are written • for indexVariable in someSequence: doSomething(variableName) … • foris 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
What 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 top of the for statement, where the index variable gets set to the next item in the sequence • Repeat until the sequence is exhausted
getPixels returns a sequence • getPixels 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 ingetPixels(picture): value=getRed(p) setRed(p,value*0.5)
Do we need value? • Not really: Remember that we can swap names for data or functions that are equivalent. def decreaseRed(picture): for p ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) def decreaseRed(picture): for p ingetPixels(picture): setRed(p, getRed(p) *0.5)
Let’s walk that through slowly… Here we get a picture object in as input and call it picture def decreaseRed(picture): for p ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) picture
Now, 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 ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) picture getPixels() Pixel, color r=135 g=116b=48 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … p
Get the red value from pixel def decreaseRed(picture): for p ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) We get the red value of pixel p and name it value picture getPixels() Pixel, color r=135 g=116b=48 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … … value = 135 p
Now change the pixel def decreaseRed(picture): for p ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) Set the red value of pixel p to 0.5 (50%) of value picture getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 135 p
Then move on to the next pixel def decreaseRed(picture): for p ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) Move on to the next pixel and name itp picture getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 135 p
Get its red value Get its red value def decreaseRed(picture): for p ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) Change value to the new red value at the new p picture getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=133g=114 b=46 Pixel, color r=134 g=114b=45 … value = 133 p p p
And change this red value def decreaseRed(picture): for p ingetPixels(picture): value=getRed(p) setRed(p,value*0.5) Change the red value at pixel p to 50% of value picture getPixels() Pixel, color r=67 g=131 b=105 Pixel, color r=66g=114 b=46 Pixel, color r=134 g=114b=45 … value = 133 p p p
And eventually, we do all pixels • We go from this… to this!
“Tracing/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. • It’s one of the most important debugging skills you can have. • And everyone has to do a lot of debugging, especially at first.
Did 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? • Let’s check it!
>>> file = pickAFile() >>> print file C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\barbara.jpg >>> pict = makePicture(file) >>> pixel = getPixel(pict,1,1) >>> print pixel Pixel, color=color r=168 g=131 b=105 >>> decreaseRed(pict) >>> newPixel = getPixel(pict,1,1) >>> print newPixel Pixel, color=color r=84 g=131 b=105 >>> print 168 * 0.5 84.0
If 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 don’t specify a full path,will be saved in the same directory as JES.
Bonus Material • Swap colors • Change Frylock’s color
Swap Colors • Goal: Let’s try to swap the red and blue channels • How to do this?
Swap Colors Code def swapColors(picture): for thisPixel in getPixels(picture): r = getRed(thisPixel) b = getBlue(thisPixel) setBlue(thisPixel, r) setRed(thisPixel, b) repaint(picture)
Change Frylock’s Color • Want to replace Frylock’s color from red to some other color, such as green • Need to test each pixel to see if it is close to Frylock’s color
Testing a Pixel • Need to use a new Jython keyword: if • Example of “if”: if 3 < 5: print “3 is less than 5” • We can check if a pixel is similar to another by using the distance function
Frylock Color Change Code def frylockColorChange(picture): frylockColor = makeColor(224, 45, 49) for pixel in getPixels(picture): thisDistance = distance(frylockColor, getColor(pixel)) if thisDistance < 30: setColor(pixel, green)
Demonstrating: Manipulating Colors >>> print color color r=81 g=63 b=51 >>> print newcolor color r=255 g=51 b=51 >>> print distance(color,newcolor) 174.41330224498358 >>> print color color r=168 g=131 b=105 >>> print makeDarker(color) color r=117 g=91 b=73 >>> print color color r=117 g=91 b=73 >>> newcolor=pickAColor() >>> print newcolor color r=255 g=51 b=51 >>> print getRed(pixel) 168 >>> setRed(pixel,255) >>> print getRed(pixel) 255 >>> color=getColor(pixel) >>> print color color r=255 g=131 b=105 >>> setColor(pixel,color) >>> newColor=makeColor(0,100,0) >>> print newColor color r=0 g=100 b=0 >>> setColor(pixel,newColor) >>> print getColor(pixel) color r=0 g=100 b=0
We can change pixels directly… >>> file="/Users/guzdial/mediasources/barbara.jpg" >>> pict=makePicture(file) >>> show(pict) >>> setColor(getPixel(pict,10,100),yellow) >>> setColor(getPixel(pict,11,100),yellow) >>> setColor(getPixel(pict,12,100),yellow) >>> setColor(getPixel(pict,13,100),yellow) >>> repaint(pict) But that’s really tedious… Manipulating pictures more cleverly is the next topic…
How do you find out what RGB values you have? And where? • Use the MediaTools! (especially useful when testing and debugging…)