22
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute of Technology, 2003– 2004; modified by Robert H. Sloan, University of Illinois at Chicago, 2005, for educational use.

CS 101: Introduction to Computing

Embed Size (px)

DESCRIPTION

CS 101: Introduction to Computing. Programming picture manipulations. Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan, University of Illinois at Chicago, 2005, for educational use. Demonstrating: Manipulating Colors. >>> print color - PowerPoint PPT Presentation

Citation preview

Page 1: CS 101: Introduction to Computing

CS 101:Introduction to Computing

Programming picture manipulations

Developed by Mark Guzdial, Georgia Institute of Technology, 2003–2004; modified by Robert H. Sloan, University of Illinois at Chicago, 2005, for educational use.

Page 2: CS 101: Introduction to Computing

Demonstrating: Manipulating Colors>>> print getRed(pixel)168>>> setRed(pixel,255)>>> print getRed(pixel)255>>> color=getColor(pixel)>>> print colorcolor r=255 g=131 b=105>>> setColor(pixel,color)>>> newColor=makeColor(0,100,0)>>> print newColorcolor r=0 g=100 b=0>>> setColor(pixel,newColor)>>> print getColor(pixel)color r=0 g=100 b=0

>>> print colorcolor r=81 g=63 b=51>>> print newcolorcolor r=255 g=51 b=51>>> print distance(color,newcolor)174.41330224498358>>> print colorcolor r=168 g=131 b=105>>> print makeDarker(color)color r=117 g=91 b=73>>> print colorcolor r=117 g=91 b=73>>> newcolor=pickAColor()>>> print newcolorcolor r=255 g=51 b=51

Page 3: CS 101: Introduction to Computing

We can change pixels directly…>>> file="/Users/sloan/mediasources/MauryBaby.jpg">>> pict=makePicture(file)>>> show(pict)>>> setColor(getPixel(pict,10,160), blue)>>> setColor(getPixel(pict,11,160), blue)>>> setColor(getPixel(pict,12,160), blue)>>> setColor(getPixel(pict,13,160), blue)>>> repaint(pict)

But that’s really tedious…Manipulating pictures more cleverlyis our next topic!

Page 4: CS 101: Introduction to Computing

How do you find out what RGB values you have? And where?

Use the MediaTools!

(especially useful whentesting and debugging…)

Page 5: CS 101: Introduction to Computing

Use a loop!Our first picture recipe

def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

Used like this:>>> file="/Users/sloan/Mediasources/Tonks.jpg">>> pic=makePicture(file)>>> show(pic)>>> decreaseRed(pic)>>> repaint(pic)

QuickTime™ and aTIFF (LZW) decompressor

are needed to see this picture.

Note: the name picture takes on the value of pic

Page 6: CS 101: Introduction to Computing

How 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 that is indented e.g. for x in range(1,100):

print x

Page 7: CS 101: Introduction to Computing

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 for statement, where the index variable gets set to the next item in the sequence

Repeat until the sequence is exhausted.

for x in range(1,100): print x

output: 1 2 3 ...

Page 8: CS 101: Introduction to Computing

getPixels returns a sequence of pixels Each pixel knows its color and its location in the

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): value=getRed(p) setRed(p,value*0.5)

Page 9: CS 101: Introduction to Computing

Do 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): setRed(p, getRed(p) *0.5)

Page 10: CS 101: Introduction to Computing

Let’s walk through that slowly…def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

Here we get a picture object in as input and give it the variable name picture

picture

Page 11: CS 101: Introduction to Computing

Now, get the pixelsdef decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

We get all the pixels from the picture, then make p be the name of each one one at a time

picture

Pixel, color r=168 g=131 b=105

Pixel, color r=160 g=131 b=105

Pixel, color r=168 g=132b=106

p

getPixels()

Page 12: CS 101: Introduction to Computing

Get the red value from pixeldef decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

We get the red value of pixel p and name it value

picture

Pixel, color r=168 g=131 b=105

Pixel, color r=168 g=132b=106

pvalue = 168

Pixel, color r=160 g=131 b=105

Page 13: CS 101: Introduction to Computing

Now change the pixeldef decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

Set the red value of pixel p to 0.5 (50%) of value

picture

Pixel, color r=84 g=131 b=105

Pixel, color r=168 g=132b=106

pvalue = 168

Pixel, color r=160 g=131 b=105

Page 14: CS 101: Introduction to Computing

Then move on to the next pixeldef decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

Move on to the next pixel and name it p

picture

Pixel, color r=84 g=131 b=105

Pixel, color r=168 g=132b=106

pvalue = 168

Pixel, color r=160 g=131 b=105

Page 15: CS 101: Introduction to Computing

Get its red valuedef decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

Change value to the new red value at the new p

picture

Pixel, color r=84 g=131 b=105

Pixel, color r=168 g=131 b=105

Pixel, color r=168 g=132b=106

value = 160p

Get its red valuedef decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5)

Change value to the new red value at the new p

picture

Pixel, color r=84 g=131 b=105

Pixel, color r=168 g=132b=106

p

Pixel, color r=160 g=131 b=105

Page 16: CS 101: Introduction to Computing

And change this red value

def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Change the red value at

pixel p to 50% of valuepicture

Pixel, color r=84 g=131 b=105

Pixel, color r=80 g=131 b=105

Pixel, color r=168 g=132b=106

value = 160p

Page 17: CS 101: Introduction to Computing

And eventually, we do all of the pixels...

We go from this… to this!

Page 18: CS 101: Introduction to Computing

“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.

Page 19: CS 101: Introduction to Computing

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!

Page 20: CS 101: Introduction to Computing

>>> file = pickAFile()>>> print file/Users/sloan/Mediasources/Tonks.jpg>>> pict = makePicture(file)>>> pixel = getPixel(pict,145,17)>>> print pixelPixel, color=color r=236 g=98 b=147>>> decreaseRed(pict)>>> print pixelPixel, color=color r=236 g=98 b=147 Still the old pixel>>> newPixel = getPixel(pict, 145,17)>>> print newPixelPixel, color=color r=118 g=131 b=105>>> print 236 * 0.5118.0

Page 21: CS 101: Introduction to Computing

Checking it in the MediaTools

Page 22: CS 101: Introduction to Computing

If you make something you like…

writePictureTo(picture, “filename”) Writes the picture out as a JPEG Be sure to end your filename as “.jpg”!

e.g. myPic.jpg

If you don’t specify a full path,will be saved in the same directory as JES.