Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 177 Week 4 Recitation Slides for Loop if statement and range.

Similar presentations


Presentation on theme: "1 CS 177 Week 4 Recitation Slides for Loop if statement and range."— Presentation transcript:

1 1 CS 177 Week 4 Recitation Slides for Loop if statement and range

2 2 Announcements EXAM 1  Wednesday 09/29  6:30p - 7:30p  EE 129

3 3 ANY QUESTIONS?

4 4 Let’s remember for Loop def decreaseRed(picture): for p in getPixels(picture): value = getRed(p) setRed(p,value*0.5)

5 5 What is wrong here? def decreaseRed(picture): for p in getPixels(picture): value = getRed(p) setRed(p,value*0.5) Indentation is wrong! This statement is not inside the for loop. Only the last pixel is changed.

6 Clearing Blue def clearBlue(picture): for p in getPixels(picture): setBlue(p,0) Again, this will work for any picture. 6

7 Lightening and darkening an image def darken(picture): for px in getPixels(picture): color = getColor(px) color = makeDarker(color) setColor(px,color) 7 def lighten(picture): for px in getPixels(picture): color = getColor(px) color = makeLighter(color) setColor(px,color)

8 Creating a negative Let’s think it through  R,G,B go from 0 to 255  Let’s say Red is 10. That’s very light red. What’s the opposite? LOTS of Red!  The negative of that would be 255 – 10 = 245 So, for each pixel, if we negate each color component in creating a new color, we negate the whole picture. 8

9 Creating a negative def negative(picture): for px in getPixels(picture): red = getRed(px) green = getGreen(px) blue = getBlue(px) negColor=makeColor( 255-red, 255-green, 255-blue) setColor(px,negColor) 9 negative of negative is the original picture

10 Converting to greyscale We know that if red=green=blue, we get grey  But what value do we set all three to? What we need is a value representing the darkness of the color, the luminance There are lots of ways of getting it, but one way that works reasonably well is really simple—simply take the average: 10

11 Converting to greyscale def greyScale(picture): for p in getPixels(picture): intensity = (getRed(p)+getGreen(p)+getBlue(p))/3 setColor(p,makeColor(intensity,intensity,intensity)) 11

12 Building a better greyscale def greyScaleNew(picture): for px in getPixels(picture): newRed = getRed(px) * 0.299 newGreen = getGreen(px) * 0.587 newBlue = getBlue(px) * 0.114 luminance = newRed + newGreen + newBlue setColor(px,makeColor(luminance,luminance,luminance)) 12 We’ll weight red, green, and blue based on how light we perceive them to be, based on laboratory experiments.

13 Comparing the two greyscales: Average on left, weighted on right 13

14 How to save the changes? writePictureTo(picture,”filename”) Windows:  writePictureTo(picture,"E:/temp/output.jpg") MacOS  writePictureTo(picture,"/home/users/guzdial/med iasources/output.jpg") 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. 14

15 if statement An if statement takes a logical expression and evaluates it. If it is true,the statements in if block are executed, otherwise, they are not executed. if a < 100: print "a is small“ 15 If a is 45, prints “a is small” If a is 153, does nothing

16 if - else statement Similarly, the logical expression is evaluated. If it is true,the statements in if block are executed, otherwise, the statements in else block are executed. if a < 100: print "a is small" else: print "a is large" 16 If a is 45, prints “a is small” If a is 153, prints “a is large”

17 Let’s count the red pixels in a picture def countRedPixels(picture): redCount = 0 for p in getPixels(picture): color = getColor(p) if(color == red): redCount = redCount + 1 print redCount 17

18 Let’s count the the non-red pixels too def countPixels(picture): redCount = 0 nonRedCount = 0 for p in getPixels(picture): color = getColor(p) if(color == red): redCount = redCount + 1 else: nonRedCount = nonRedCount + 1 print redCount print nonRedCount 18

19 function range Range is a function that returns a sequence If range has only one input parameter: (i.e range(input))  It generates the sequence of all the non-negative integers that are less than the input parameter value  the generated sequence starts with 0  increment is 1  the last element of the sequence is the value of input parameter – 1 >>> range(3) >>> range(1)>>> range(-1) [0,1,2] [0] [] >>> range(9) >>> range(0) >>> range(-5) [0, 1, 2, 3, 4, 5, 6, 7, 8][] [] 19

20 function range If two inputs (i.e range(first_input, second_input)):  It generated the sequence of all the integers that are greater than or equal to the first_input value and less than the second_input value  the first element of the sequence is the value of first_input  increment is 1  the last element of the sequence is the value of second_input – 1 >>> range(0, 3) >>> range(4, 7)>>> range(-2, 2) [0, 1, 2] [4, 5, 6] [-2, -1, 0, 1] >>> range(0, 10) >>> range(7, 4) >>> range(-2, -5) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][] [] 20

21 function range If three inputs (i.e. range(first_input, second_input, third_input)):  the sequence starts with the first_input value  increment is third-input  If increment is positive the sequence ends with the largest value less than second_input  If increment is negative the sequence ends with the smallest value greater than second_input >>> range(0, 3, 1) >>> range(1, 7, 2) >>> range(-5, 5, 3) [0, 1, 2] [1, 3, 5] [-5, -2, 1, 4] >>> range(0, 6, 3) >>> range(-7, -1, 2) >>> range(7, 1, -2) [0, 3] [-7, -5, -3] [7, 5, 3] 21

22 22 Final QUESTIONS???


Download ppt "1 CS 177 Week 4 Recitation Slides for Loop if statement and range."

Similar presentations


Ads by Google