Agenda 9/24/18 Turn in HW1 Handout HW2 Announcements:

Agenda  9/24/18  Turn in HW1  Handout HW2  Announcements:

Agenda 9/24/18 Turn in HW1 Handout HW2 Announcements: No class on Wednesday Lab Friday: Will be Alice and will be made available on Canvas May work on it on own or up to groups of 3 Finish Data Representation Python functions For loops Color change functions Pixels Pixels are picture elements Each pixel object knows its color It also knows where it is in the picture

When we zoom the picture to 500%, we can see individual pixels. for loop! def decreaseRed(pict): for p in getPixels(pict): originalRed = getRed(p) setRed(p, originalRed * 0.5) The loop - Note the indentation! How for loops are written

def decreaseRed(pict): for p in getPixels(pict): originalRed = getRed(p) setRed(p, originalRed * 0.5). for is the name of the command An index variable (p, in this example) is used to hold each of the different values of a sequence The word in A function that generates a sequence The index variable will be the name for one value in the sequence, each time through the loop A colon (:) And a block (the indented lines of code)

What happens when a for loop is executed The index variable is set to an item in the sequence The block is executed The variable is often used inside the block Then execution loops to the for statement, where the index variable gets set to the next item in the sequence Repeat until every value in the sequence was used. getPixels returns a sequence of pixels def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) Each pixel knows its color and place in the

original picture Change the pixel, you change the picture So the loop here assigns the index variable p to each pixel in the picture picture, one at a time. Lets walk that through slowly def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) Here we take a picture object in as a parameter to the function and call it picture picture

Now, get the pixels def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 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=135 g=131 b=105

p Pixel, color r=133 g=114 b=46 Pixel, color r=134 g=114 b=45 getPixels() keeps going for every single pixel in the entire image

Get the red value from pixel def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) We get the red value of pixel p and name it originalRed picture Pixel, color r=135 g=131 b=105 p

Pixel, color r=133 g=114 b=46 Pixel, color r=134 g=114 b=45 getPixels() originalRed = 135

Now change the pixel def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) Set the red value of pixel p to 0.5 (50%) of originalRed picture Pixel, color r=67 g=131 b=105 p

Pixel, color r=133 g=114 b=46 Pixel, color r=134 g=114 b=45 getPixels() originalRed = 135 Then move on to the next pixel

def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) Move on to the next pixel and name it p picture Pixel, color r=67 g=131 b=105 Pixel, color r=133

g=114 b=46 p Pixel, color r=134 g=114 b=45 getPixels() originalRed = 135 Get its red value Set originalRed to the red value at the new p, then

change the red at that new pixel. def decreaseRed(picture): for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) picture Pixel, color r=67 g=131 b=105 Pixel, color r=133

g=114 b=46 p Pixel, color r=134 g=114 b=45 getPixels() originalRed = 133 And change this red value def decreaseRed(picture):

for p in getPixels(picture): originalRed = getRed(p) setRed(p, originalRed * 0.5) Change the red value at pixel p to 50% of value picture Pixel, color r=67 g=131 b=105 Pixel, color r=66 g=114

b=46 p Pixel, color r=134 g=114 b=45 getPixels() originalRed= 133 And eventually, we do all pixels We go from this to this!

Tracing/Walking through the program What we just did is called tracing, stepping or walking through the program. You may hear them interchangeably. What does this mean? You consider each step of the program, in the order that the computer would execute it You consider what exactly would happen You create a location for each variable 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. Command Area Editing Tips Up/down arrows walk through command history You can edit the line at the bottom Just put the cursor at the end of the line before hitting Return/Enter. If line disappears lengthen the bottom of the window Anytime you hit load, it saves the program to your directory Blocking is important in JES Statements that are

indented the same, are in the same block. Statements that are in the same block as where the line where the cursor is are enclosed in a blue box. Forgetting to load Your function does NOT exist for JES until you load it Before you load it, the program is just a bunch of

characters. Loading encodes it as an executable function Save and Save As You must Save before Loading You must Load before you can use your function An Unloaded function doesnt exist yet. What went wrong? Did you use the exact same names (case,

spelling)? All the lines in the block must be indented, and indented the same amount. Variables in the command area dont exist in your functions, and variables in your functions dont exist in the command area. Colon! The computer cant read your mind. It will only do exactly what you tell it to do. Review of JES Functions A bunch of functions are pre-defined in JES for sound and picture manipulations

pickAFile() makePicture() makeSound() show() play() Some of these functions accept input values. Which ones? Can you think of others? What do these functions do? >>> print pickAFile() /Users/guzdial/mediasources/barbara.jpg >>> print makePicture(pickAFile()) Picture, filename /Users/guzdial/mediasources/barbara.jpg height 294 width 222

>>> show(makePicture(pickAFile())) >>> print show(makePicture(pickAFile())) None >>> print pickAFile() /Users/guzdial/mediasources/hello.wav >>> print makeSound(pickAFile()) Sound of length 54757 >>> print play(makeSound(pickAFile())) None pickAFile() returns a filename, which can be used as input to makePicture() to make a picture or makeSound() to make a sound. Printing a picture just proves theres a picture there. show() and play() dont return

anything, so they print None. Reminder: Typical sequence of commands to choose and show an image >>> file=pickAFile() >>> picture=makePicture(file) >>> show(picture) Manipulating pixels getPixel(picture,x,y) gets a single pixel. getPixels(picture) gets all of them in an array. (Square brackets is a standard array reference notationwhich we'll generally not use.) >>> pixel=getPixel(picture,1,1) >>> print pixel Pixel, color=color r=168 g=131 b=105 >>> pixels=getPixels(picture)

>>> print pixels[0] Pixel, color=color r=168 g=131 b=105 What can we do with a pixel? getRed, getGreen, and getBlue are functions that take a pixel as input and return a value between 0 and 255 setRed, setGreen, and setBlue are functions that take a pixel as input and a value between 0 and 255 getColor takes a pixel as input and returns a Color object with the color at that pixel setColor takes a pixel as input and a Color, then sets the pixel to that color makeColor takes red, green, and blue values (in that order) between 0 and 255, and returns a Color object pickAColor lets you use a color chooser and returns the chosen color We also have functions that can makeLighter and makeDarker an input color

Demonstrating: Manipulating Colors >>> print getRed(pixel) 168 >>> color=getColor(pixel) >>> setRed(pixel,255) >>> print color color r=255 g=131 b=105 >>> print getRed(pixel) 255 >>> 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 dull and boring to change each pixel at a time

Isn't there a better way? This is why we use a loop! def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) Used like this: >>> file="/Users/guzdial/mediasources/barbara.jpg" >>> picture=makePicture(file) >>> show(picture) >>> decreaseRed(picture) >>> repaint(picture) This works for any image. What do we need to change to make it work for a new image?

Clearing Blue def clearBlue(picture): for p in getPixels(picture): setBlue(p,0) Again, this will work for any picture. Try stepping through this one yourself! Which one of the below pictures was generated from this function: def change2(picture): for pixel in getPixels(picture): setBlue(pixel,0) Which one of the below pictures was generated from this function:

def change3(picture): for pixel in getPixels(picture): setGreen(pixel,2 * getGreen(pixel)) Can we combine these? Why not! How do we turn this beach scene into a sunset? What happens at sunset? At first, I tried increasing the red, but that made things like red specks in the sand REALLY prominent. That can't be how it really works New Theory: As the sun sets,

less blue and green is visible, which makes things look more red. A Sunset-generation Function def makeSunset(picture): for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) value=getGreen(p) setGreen(p,value*0.7) Contrast these two programs def makeSunset(picture): for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) value=getGreen(p)

setGreen(p,value*0.7) Yes, they do the exact same thing! makeSunset(somepict) works the same in both cases def makeSunset(picture): reduceBlue(picture) reduceGreen(picture) def reduceBlue(picture): for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) def reduceGreen(picture): for p in getPixels(picture): value=getGreen(p) setGreen(p,value*0.7)

Observations on the new makeSunset It's okay to have more than one function in the same Program Area (and file) makeSunset in this one is somewhat easier to read. It's clear what it does reduceBlue and reduceGreen That's important! def makeSunset(picture): reduceBlue(picture) reduceGreen(picture) def reduceBlue(picture):

for p in getPixels(picture): value=getBlue(p) setBlue(p,value*0.7) def reduceGreen(picture): for p in getPixels(picture): value=getGreen(p) setGreen(p,value*0.7) Programs are written for people, not computers! Does makeSunset do one and only one thing? Yes, but it's a higher-level, more abstract thing. It's built on lower-level one and only one thing We call this hierarchical decomposition. You have some thing that you want the computer to do? Redefine that thing in terms of smaller things

Repeat until you know how to write the smaller things Then write the larger things in terms of the smaller things. I want to increase the blue of each pixel by 25%. I write this: def increaseBlue(pic): for pixel in getPixels(pic): b = getBlue(pixel) ## What goes here? setBlue(pixel, 0.25 * b) setBlue(pixel, 1.25 * b) setBlue(pixel, 0.75 * b) setBlue(pixel, 1.75 * b) I want to increase the red of each pixel by the

same amount. I write this: def increaseRed(pic): for pixel in getPixels(pic): r = getRed(pixel) ## What goes here? setRed(pixel, r - 50) setRed(pixel, r / 50) setRed(pixel, r * 50) setRed(pixel, r + 50) Lightening and darkening an image def lighten(picture): for px in getPixels(picture): color = getColor(px) color = makeLighter(color) setColor(px ,color)

def darken(picture): for px in getPixels(picture): color = getColor(px) color = makeDarker(color) setColor(px ,color) Consider these two functions def decreaseRed(picture): for p in getPixels(picture): value=getRed(p) setRed(p,value*0.5) def decreaseRedNew(picture, amount): for p in getPixels(picture): value=getRed(p) setRed(p,value*amount)

First, it's perfectly okay to have multiple inputs to a function. The new function, decreaseRedNew, now takes an input of the multiplier for the red value. decreaseRedNew(picture, 0.5) would do the same thing decreaseRedNew(picture, 1.25) would increase red 25% Functions In natural language, the same word has different meanings depending on context. Im going to fly to Vegas. Would you please swat that fly? A function is its own context. Input variables (placeholders) take on the value of the input values only for the life of the function, i.e. only while its executing

Variables defined within a function also only exist within the context of that function The context of a function is also called its scope Input variables are placeholders Think of the input variable as a placeholder It takes the place of the input object During the time that the function is executing, the placeholder variable stands for the input object. When we modify the placeholder by changing its pixels with setRed, we actually change the input object.

Recently Viewed Presentations

  • 2011-2012 FIMC-VI Webinar Series Introduction to FIMC-VI for

    2011-2012 FIMC-VI Webinar Series Introduction to FIMC-VI for

    American Printing House for the Blind. In 1879 the Congress of the United States passed the Act to Promote the Education of the Blind. This act designates APH as the official supplier of educational materials to all students in the...
  • ***Delete these slides after reviewing*** Facilitator notes: Print

    ***Delete these slides after reviewing*** Facilitator notes: Print

    What tools do you use? What is the goal of the meeting? Time frame, communication, criteria for home, access to data, info, forms & homes, discuss service expectations, Always meet at the office the office for 1st meeting, then we...
  • Latin and Greek Roots

    Latin and Greek Roots

    WEEK ONE WORD LIST root meaning examples anti against antibody, antithesis, antifreeze bi two bilateral, bicycle, biped, binocular com together comfort, complete, common, combination ...
  • Fifth IABIN Council Meeting May 7-11, 2007 Punta del Este ...

    Fifth IABIN Council Meeting May 7-11, 2007 Punta del Este ...

    Red Temática de Áreas Protegidas Progreso Julio 2006 - Abril 2007 Elaboración de los Términos de Referencia para la producción de documentos claves para las actividades de la red Contacto con los puntos focales Designación de un representante técnico para...
  • Hearin - Westerville City School District

    Hearin - Westerville City School District

    Sensory fibers contained within the vestibular nuclei 4 functions of the 2 vestibular nuclei. ... Info then goes to opposite side of brain to processing center which coordinates reflexes such as turning your head from a loud noise. ... Hearin...
  •  common core  PARCC  UDL  smarter balanced   RTI  ELL

    common core PARCC UDL smarter balanced RTI ELL

    The Picture Dictionary in Read&Write is a great tool to include students of all learning abilities in the classroom conversation. Not only does the Picture Dictionary help those students who are learning English as a second language or who have...
  • Strategy Formulation: Functional Strategy and Strategic Choice Chapter

    Strategy Formulation: Functional Strategy and Strategic Choice Chapter

    (who may be an individual or a group) is one who is assigned to identify potential pitfalls and problems with a proposed alternative strategy in a formal presentation. When applied to strategic decision making, dialectical inquiry . requires that two...
  • Chapter 9 Strategy Review - College of Micronesia-FSM

    Chapter 9 Strategy Review - College of Micronesia-FSM

    Appraisal of Strategic Performance Ch 9 -* Strategy Review, Evaluation, and Control Consonance Consistency Feasibility Advantage Rumelt's 4 Criteria Ch 9 -* Strategy Review, Evaluation, and Control Strategy should not present inconsistent goals and policies Consistency Ch 9 -* Strategy...