1 / 22

CS 177 Week 4 Recitation Slides

CS 177 Week 4 Recitation Slides. for Loop if statement and range. Announcements. EXAM 1 Wednesday 09/29 6:30p - 7:30p EE 129. ANY QUESTIONS?. Let’s remember for Loop. def decreaseRed(picture): for p in getPixels(picture) : value = getRed( p ) setRed( p ,value*0.5). 4.

jovan
Download Presentation

CS 177 Week 4 Recitation Slides

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. ANY QUESTIONS?

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

  5. What is wrong here? def decreaseRed(picture): forpin 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. 5

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

  7. Lightening and darkening an image def darken(picture): for px in getPixels(picture): color = getColor(px) color = makeDarker(color) setColor(px ,color) 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.

  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) 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:

  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))

  12. Building a better greyscale We’ll weight red, green, and blue based on how light we perceive them to be, based on laboratory experiments. 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))

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

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

  15. if statement If a is 45, prints “a is small” If a is 153, does nothing 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“

  16. if - else statement If a is 45, prints “a is small” If a is 153, prints “a is large” 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"

  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

  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

  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 parametervalue • 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] [] []

  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] [] []

  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]

  22. Final QUESTIONS???

More Related