Conditionals in Pictures

22
03-ConditionalsInPictures Barb Ericson Georgia Institute of Technology Oct 2010 Conditionals in Pictures

description

Conditionals in Pictures. Barb Ericson Georgia Institute of Technology Oct 2010. Learning Goals. What are Boolean expressions? How does Python represent true and false? How to conditionally execute code? How to create a conditional with two options? - PowerPoint PPT Presentation

Transcript of Conditionals in Pictures

Page 1: Conditionals in Pictures

03-ConditionalsInPictures

Barb Ericson

Georgia Institute of Technology

Oct 2010

Conditionals in Pictures

Page 2: Conditionals in Pictures

03-ConditionalsInPictures

Learning Goals

• What are Boolean expressions?• How does Python represent true and false?• How to conditionally execute code?• How to create a conditional with two options?• How to create a conditional with more than two

options?• How to create complex Boolean expressions

joined with 'and' and 'or'?

Page 3: Conditionals in Pictures

Boolean Expressions

• Try the following in JES:

>>> 3 < 5

>>> 3 == 3

>>> 3 > 5

>>> 6 > 2

>>> 3 >= 2

>>> 2 <= 3

>>> 3 >= 3

What value is used for true and what value for false?

03-ConditionalsInPictures

Page 4: Conditionals in Pictures

Expressions

• Can test equality with === means "make them equal"

== "are they equal?"

• Can also test <, >, >=, <=, <> (not equals)

• In general, 0 is false, 1 is true– So you can have a function return a “true” or

“false” value.

03-ConditionalsInPictures

Page 5: Conditionals in Pictures

Removing “Red Eye”• When the flash of the camera

catches the eye just right (especially with light colored eyes), we get bounce back from the back of the retina.

• This results in “red eye”• We can replace the “red” with

a color of our choosing.• First, we figure out where the

eyes are (x,y) using the explorer

– explore(pict)

03-ConditionalsInPictures

Page 6: Conditionals in Pictures

How an if (conditional) works

• if is the command name• Next comes an expression:

Some kind of true or false comparison

• Then a colon

• Then the body of the if which is the things that will happen if the expression is true

if distance(color, brown) < 50.0: redness=getRed(px)*1.5 blueness=getBlue(px) greenness=getGreen(px)

03-ConditionalsInPictures

Page 7: Conditionals in Pictures

Removing Red Eye

def removeRedEye(pic,startX,startY,endX,endY,replacementcolor): red = makeColor(255,0,0) for x in range(startX,endX): for y in range(startY,endY): currentPixel = getPixel(pic,x,y) if (distance(red,getColor(currentPixel)) < 165): setColor(currentPixel,replacementcolor)

What we’re doing here:

• Within the rectangle of pixels (startX,startY) to (endX, endY)

• Find pixels close to red, then replace them with a new color

Why use a range? Because we don’t want to replace her red dress!

03-ConditionalsInPictures

Page 8: Conditionals in Pictures

“Fixing” it: Changing red to black

removeRedEye(jenny, 109, 91, 202, 107, makeColor(0,0,0))

• Jenny’s eyes are actually not black—could fix that• Eye are also not mono-color

– A better function would handle gradations of red and replace with gradations of the right eye color

03-ConditionalsInPictures

Page 9: Conditionals in Pictures

Challenge

• Try to change one color in a range to another color– Like change the hair in the picture of a person – Or the clothes

03-ConditionalsInPictures

Page 10: Conditionals in Pictures

Generating sepia-toned prints

• Pictures that are sepia-toned have a yellowish tint to them that we associate with older photographs.

• It’s not just a matter of increasing the amount of yellow in the picture, because it’s not a one-to-one correspondence.– Instead, colors in different ranges get converted to

other colors.– We can create such convertions using if

03-ConditionalsInPictures

Page 11: Conditionals in Pictures

Example of sepia-toned prints

03-ConditionalsInPictures

Page 12: Conditionals in Pictures

Here’s how we do itdef sepiaTint(picture): #Convert image to grayscale grayscale(picture)

#loop through picture to tint pixels

for p in getPixels(picture): red = getRed(p) blue = getBlue(p)

#tint shadows if (red < 63): red = red*1.1 blue = blue*0.9

#tint midtones if (red > 62 and red < 192): red = red*1.15 blue = blue*0.85

#tint highlights if (red > 191): red = red*1.08 if (red > 255): red = 255

blue = blue*0.93

#set the new color values setBlue(p, blue) setRed(p, red)

Bug alert!Make sure you indent the right amount

03-ConditionalsInPictures

Page 13: Conditionals in Pictures

Chroma Key – Blue Screen

• For TV and movie special effects they use a blue or green screen– Here just a blue sheet was

used– Professionally you need an

evenly lit, bright, pure blue background

• With nothing blue in the scene

03-ConditionalsInPictures

Page 14: Conditionals in Pictures

Chromakey Function

def chromakey(source ,bg):

# source should have something in front of blue

# bg is the new background

for x in range(0, getWidth(source )):

for y in range(0, getHeight(source )):

p = getPixel(source ,x,y)

# if red + green < blue

if (getRed(p) + getGreen(p) < getBlue(p)):

setColor(p,getColor(getPixel(bg ,x,y)))

return source

03-ConditionalsInPictures

Page 15: Conditionals in Pictures

Testing chromakey

markP = makePicture(getMediaPath("blue-mark.jpg"))

newBack =

makePicture(getMediaPath("moon-surface.jpg"))

chromakey(markP,newBack)

explore(markP)

03-ConditionalsInPictures

Page 16: Conditionals in Pictures

Chromakey with Distance to a Color

def chromakeyGreen(source ,bg, backColor, limit):

# source should have something in front of green

# bg is the new background

for x in range(0, getWidth(source )):

for y in range(0, getHeight(source )):

p = getPixel(source ,x,y)

# if distance to the color is less than limit

if (p.colorDistance(backColor) < limit)

setColor(p,getColor(getPixel(bg ,x,y)))

return source03-ConditionalsInPictures

Page 17: Conditionals in Pictures

How many when there is an “And”?

• I want you to get soup, milk, bread, and yogurt at the store.– How many items will you come home with?

• I want you to clean your room and mop the floor in the kitchen and wash the dishes.– How many tasks do you need to do?

• I want a scoop of chocolate scoop and a scoop of vanilla.– How many scoops of ice cream is this?

03-ConditionalsInPictures

Page 18: Conditionals in Pictures

How many when there is an “Or”

• You need to help clean the house– You can clean the bathroom or the kitchen or the

living room– How many jobs do you have to do?

• You want to get an ice cream– The flavors you can pick from are chocolate, vanilla,

strawberry, or orange sherbet– How many flavors do you need to pick for a single

scoop?

03-ConditionalsInPictures

Page 19: Conditionals in Pictures

Truth TableConditional Operand 1 Operand 2 Result

And true true true

And true false false

And false true false

And false false false

Or true true true

Or true false true

Or false true true

Or false false false

Exclusive Or true true false

Exclusive Or true false true

Exclusive Or false true true

Exclusive Or false false false

03-ConditionalsInPictures

Page 20: Conditionals in Pictures

Conditional Exercise

• When are the following true? When are they false?– You can go out if your room is clean and you did your

homework– You can go out if your room is clean or you did your

homework– You can go out if either your room is clean or you did

your homework but not if both of these is true

03-ConditionalsInPictures

Page 21: Conditionals in Pictures

Challenge

• Modify the general copy function to check that the targetX and targetY are within the width and height of the target picture before trying to copy the color from the source pixel to the target pixel.

03-ConditionalsInPictures

Page 22: Conditionals in Pictures

Summary

• You can execute code when some condition is true– Using an if– Or if and else

• You can have as many possibilities as you want– Add additional if's like on sepia tint

• You can combine Boolean expressions with 'and' and 'or'

03-ConditionalsInPictures