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

  • Hierarchical Perceptual Organization Ruth Kimchi University of Haifa

    Hierarchical Perceptual Organization Ruth Kimchi University of Haifa

    Perceptual Relations between Parts and Wholes. Visual objects can be viewed as hierarchical structure of parts and wholes. Structuralist: rooted firmly in British Empiricism, perceptions are constructed from atoms of elementary, unrelated local sensations that are unified by associations due...
  • B2 B CHAPTER 11 - BA 303 sup

    B2 B CHAPTER 11 - BA 303 sup

    Remember B2B COMPETIITIVE ADVANTAGE: SOURCE, POSITION, OUTCOMES AND REINVEST US AEROSPACE AND BOEING EXAMPLES UNDERSTANDING CORE COMPETENCIES Sales & Profit Life Cycles Four Introductory Marketing Strategies Maturity Stage Market Modification Product Modification Marketing-Mix Modification Decline Stage Increase investment Resolve uncertainties...
  • El Alhambra - TeacherTube

    El Alhambra - TeacherTube

    Many additions were made to the Alhambra among the 260 years the kings of the Nasrid dynasty occupied it. When ferdinand and Isabella captured the city of Granada, they lived in the castle for a while. Then, in 1812 when...
  • Cellular Automata Biologically Inspired Computing Various credits for

    Cellular Automata Biologically Inspired Computing Various credits for

    Langton's Loops Langton's Loop 0 - Background cell state 3, 5, 6 - Phases of reproduction 1 - Core cell state 4 - Turning arm left by 90 degrees 2 - Sheath cell state state 7 - Arm extending forward...
  • Powered Industrial Trucks (29 DFR 1910.178)

    Powered Industrial Trucks (29 DFR 1910.178)

    29 CFR Part 1910 Subpart D. Walking - Working Surfaces. Presented by: ETTABureau, (919) 807-2875. Rev 7/2016. The information in this presentation is provided voluntarily by the N.C. Department of Labor, Education Training and Technical Assistance Bureau as a public...
  • AMS Certified Consulting Meteorologist (CCM) Program

    AMS Certified Consulting Meteorologist (CCM) Program

    Still 276 active CCMs. In 2013 US private meteorology industry earned revenues in excess of $3 billion dollars and growing. Your Competitors are CCMs. Check the . Weather and Climate Service Providers Directory. Some Government contracts are calling for CCMs....
  • Photosynthesis and Respiration - Colorado FFA

    Photosynthesis and Respiration - Colorado FFA

    The chemical equation of photosynthesis can be written in words: Six molecules of carbon dioxide plus twelve molecules of water in combination with a healthy plant and some form of light energy, to make one molecule of sugar plus six...
  • Resource Sharing of Art Materials: Theory and Practice

    Resource Sharing of Art Materials: Theory and Practice

    Waves by GDJ is licensed under CC Zero 1.0. It's so obvious that it probably doesn't need to be said, but there are also many options as far as how much participation for the ADGC membership is most workable when...