+ All Categories
Home > Documents > CS 177 Week 4 Recitation Slides

CS 177 Week 4 Recitation Slides

Date post: 07-Jan-2016
Category:
Upload: jovan
View: 17 times
Download: 0 times
Share this document with a friend
Description:
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. - PowerPoint PPT Presentation
22
1 CS 177 Week 4 Recitation Slides for Loop if statement and range
Transcript
Page 1: CS 177 Week 4 Recitation Slides

1

CS 177 Week 4 Recitation Slides

for Loopif statement

andrange

Page 2: CS 177 Week 4 Recitation Slides

2

Announcements EXAM 1

Wednesday 09/29 6:30p - 7:30p EE 129

Page 3: CS 177 Week 4 Recitation Slides

3

ANY QUESTIONS?

Page 4: CS 177 Week 4 Recitation Slides

4

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

Page 5: CS 177 Week 4 Recitation Slides

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.

Page 6: CS 177 Week 4 Recitation Slides

Clearing Bluedef clearBlue(picture): for p in getPixels(picture): setBlue(p,0)

Again, this will work for any picture.

6

Page 7: CS 177 Week 4 Recitation Slides

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)

Page 8: CS 177 Week 4 Recitation Slides

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

Page 9: CS 177 Week 4 Recitation Slides

Creating a negativedef 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

Page 10: CS 177 Week 4 Recitation Slides

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

Page 11: CS 177 Week 4 Recitation Slides

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

Page 12: CS 177 Week 4 Recitation Slides

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.

Page 13: CS 177 Week 4 Recitation Slides

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

13

Page 14: CS 177 Week 4 Recitation Slides

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.

14

Page 15: CS 177 Week 4 Recitation Slides

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

Page 16: CS 177 Week 4 Recitation Slides

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”

Page 17: CS 177 Week 4 Recitation Slides

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

Page 18: CS 177 Week 4 Recitation Slides

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

Page 19: CS 177 Week 4 Recitation Slides

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

Page 20: CS 177 Week 4 Recitation Slides

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

Page 21: CS 177 Week 4 Recitation Slides

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

Page 22: CS 177 Week 4 Recitation Slides

22

Final QUESTIONS???


Recommended