CS 101 Introduction to Computing Programming picture manipulations

  • Slides: 22
Download presentation
CS 101: Introduction to Computing Programming picture manipulations Developed by Mark Guzdial, Georgia Institute

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 >>> print get. Red(pixel) 168 >>> set. Red(pixel,

Demonstrating: Manipulating Colors >>> print color >>> print get. Red(pixel) 168 >>> set. Red(pixel, 255) >>> print get. Red(pixel) 255 >>> color=get. Color(pixel) >>> print color r=255 g=131 b=105 >>> set. Color(pixel, color) >>> new. Color=make. Color(0, 100, 0) >>> print new. Color color r=0 g=100 b=0 >>> set. Color(pixel, new. Color) >>> print get. Color(pixel) color r=0 g=100 b=0 color r=81 g=63 b=51 >>> print newcolor r=255 g=51 b=51 >>> print distance(color, newcolor) 174. 41330224498358 >>> print color r=168 g=131 b=105 >>> print make. Darker(color) color r=117 g=91 b=73 >>> print color r=117 g=91 b=73 >>> newcolor=pick. AColor() >>> print newcolor r=255 g=51 b=51

We can change pixels directly… >>> file="/Users/sloan/mediasources/Maury. Baby. jpg" >>> pict=make. Picture(file) >>> show(pict)

We can change pixels directly… >>> file="/Users/sloan/mediasources/Maury. Baby. jpg" >>> pict=make. Picture(file) >>> show(pict) >>> set. Color(get. Pixel(pict, 10, 160), blue) >>> set. Color(get. Pixel(pict, 11, 160), blue) >>> set. Color(get. Pixel(pict, 12, 160), blue) >>> set. Color(get. Pixel(pict, 13, 160), blue) >>> repaint(pict) But that’s really tedious… Manipulating pictures more cleverly is our next topic!

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

How do you find out what RGB values you have? And where? n Use the Media. Tools! (especially useful when testing and debugging…)

Use a loop! Our first picture recipe def decrease. Red(picture): for p in get.

Use a loop! Our first picture recipe def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(p, value*0. 5) Note: the name picture takes on the value of pic Used like this: >>> file="/Users/sloan/Mediasources/Tonks. jpg" >>> pic=make. Picture(file) >>> show(pic) >>> decrease. Red(pic) >>> repaint(pic)

How loops are written n n for is the name of the command An

How loops are written n n 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 ¨ n n n 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

What happens when a loop is executed n n The index variable is set

What happens when a loop is executed n n The index variable is set to the next (or first, at the beginning) item in the sequence The block is executed ¨ n n 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 n output: 1 2 3. . .

get. Pixels returns a sequence of pixels n n n Each pixel knows its

get. Pixels returns a sequence of pixels n n n 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, one at a time. def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(p, value*0. 5)

Do we need value? n Not really: Remember that we can swap names for

Do we need value? n Not really: Remember that we can swap names for data or functions that are equivalent. def decrease. Red(picture): for p in get. Pixels(picture): set. Red(p, get. Red(p) *0. 5)

Let’s walk through that slowly… def decrease. Red(picture): for p in get. Pixels(picture): value=get.

Let’s walk through that slowly… def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(p, value*0. 5) Here we get a picture object in as input and give it the variable name picture

Now, get the pixels def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p)

Now, get the pixels def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(p, value*0. 5) Pixel, color r=168 g=131 b=105 p Pixel, color r=160 g=131 b=105 Pixel, color r=168 g=132 b=106 We get all the pixels from the picture, then make p be the name of each one at a time picture get. Pixels() …

Get the red value from pixel def decrease. Red(picture): for p in get. Pixels(picture):

Get the red value from pixel def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(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 p Pixel, color r=160 g=131 b=105 Pixel, color r=168 g=132 b=106 value = 168 …

Now change the pixel def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p)

Now change the pixel def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(p, value*0. 5) Pixel, color r=84 g=131 b=105 p Pixel, color r=160 g=131 b=105 Pixel, color r=168 g=132 b=106 value = 168 Set the red value of pixel p to 0. 5 (50%) of value picture …

Then move on to the next pixel def decrease. Red(picture): for p in get.

Then move on to the next pixel def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(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=160 g=131 b=105 p Pixel, color r=168 g=132 b=106 value = 168 …

Get its red value def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p)

Get its red value def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(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 r=160 g=131 b=105 p Pixel, color r=168 g=132 b=106 value = 160 …

And change this red value def decrease. Red(picture): for p in get. Pixels(picture): value=get.

And change this red value def decrease. Red(picture): for p in get. Pixels(picture): value=get. Red(p) set. Red(p, value*0. 5) Pixel, color r=84 g=131 b=105 Pixel, color r=80 g=131 b=105 p Pixel, color r=168 g=132 b=106 value = 160 Change the red value at pixel p to 50% of value picture …

And eventually, we do all of the pixels. . . n We go from

And eventually, we do all of the pixels. . . n We go from this… to this!

“Tracing/Stepping/Walking through” the program n What we just did is called “stepping” or “walking

“Tracing/Stepping/Walking through” the program n 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. ¨ n 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? n n Sure, the picture

Did that really work? How can we be sure? n n 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 = pick. AFile() >>> print file /Users/sloan/Mediasources/Tonks. jpg >>> pict = make.

>>> file = pick. AFile() >>> print file /Users/sloan/Mediasources/Tonks. jpg >>> pict = make. Picture(file) >>> pixel = get. Pixel(pict, 145, 17) >>> print pixel Pixel, color=color r=236 g=98 b=147 >>> decrease. Red(pict) >>> print pixel Pixel, color=color r=236 g=98 b=147 Still the old pixel >>> new. Pixel = get. Pixel(pict, 145, 17) >>> print new. Pixel, color=color r=118 g=131 b=105 >>> print 236 * 0. 5 118. 0

Checking it in the Media. Tools

Checking it in the Media. Tools

If you make something you like… n n n write. Picture. To(picture, “filename”) Writes

If you make something you like… n n n write. Picture. To(picture, “filename”) Writes the picture out as a JPEG Be sure to end your filename as “. jpg”! ¨ n e. g. my. Pic. jpg If you don’t specify a full path, will be saved in the same directory as JES.