03-ConditionalsInPictures.ppt: uploaded 19 October 2010 at 2:48 pm

advertisement
Conditionals in Pictures
Barb Ericson
Georgia Institute of Technology
Oct 2010
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'?
03-ConditionalsInPictures
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
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
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
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
Removing Red Eye
def removeRedEye(pic,startX,startY,endX,endY,replacementcolor):
red = makeColor(255,0,0)
for x in range(startX,endX):
Why use a range?
for y in range(startY,endY):
Because we don’t
want to replace her
currentPixel = getPixel(pic,x,y)
if (distance(red,getColor(currentPixel)) < 165): red dress!
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
03-ConditionalsInPictures
“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
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
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-toone correspondence.
– Instead, colors in different ranges get converted to
other colors.
– We can create such convertions using if
03-ConditionalsInPictures
Example of sepia-toned prints
03-ConditionalsInPictures
Here’s how we do it
def 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
Bug alert!
Make sure you indent the right amount
#set the new color values
setBlue(p, blue)
setRed(p, red)
03-ConditionalsInPictures
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
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
Testing chromakey
markP = makePicture(getMediaPath("blue-mark.jpg"))
newBack =
makePicture(getMediaPath("moon-surface.jpg"))
chromakey(markP,newBack)
explore(markP)
03-ConditionalsInPictures
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 source
03-ConditionalsInPictures
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
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
Truth Table
Conditional
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
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
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
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
Download