Working with ranges in pictures Barb Ericson Georgia
Working with ranges in pictures Barb Ericson Georgia Institute of Technology Feb 2010 03 -Conditionals. In. Pictures
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 -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 -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 -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 -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=get. Red(px)*1. 5 blueness=get. Blue(px) greenness=get. Green(px) 03 -Conditionals. In. Pictures
Removing Red Eye def remove. Red. Eye(pic, start. X, start. Y, end. X, end. Y, replacementcolor): red = make. Color(255, 0, 0) for x in range(start. X, end. X): Why use a range? for y in range(start. Y, end. Y): Because we don’t want to replace her current. Pixel = get. Pixel(pic, x, y) if (distance(red, get. Color(current. Pixel)) < 165): red dress! set. Color(current. Pixel, replacementcolor) What we’re doing here: • Within the rectangle of pixels (start. X, start. Y) to (end. X, end. Y) • Find pixels close to red, then replace them with a new color 03 -Conditionals. In. Pictures
“Fixing” it: Changing red to black remove. Red. Eye(jenny, 109, 91, 202, 107, make. Color(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 -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 -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-toone correspondence. – Instead, colors in different ranges get converted to other colors. – We can create such convertions using if 03 -Conditionals. In. Pictures
Example of sepia-toned prints 03 -Conditionals. In. Pictures
Here’s how we do it def sepia. Tint(picture): #Convert image to grayscale(picture) #loop through picture to tint pixels for p in get. Pixels(picture): red = get. Red(p) blue = get. Blue(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 set. Blue(p, blue) set. Red(p, red) 03 -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 -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, get. Width(source )): for y in range(0, get. Height(source )): p = get. Pixel(source , x, y) # if red + green < blue if (get. Red(p) + get. Green(p) < get. Blue(p)): set. Color(p, get. Color(get. Pixel(bg , x, y))) return source 03 -Conditionals. In. Pictures
Testing chromakey mark. P = make. Picture(get. Media. Path("blue-mark. jpg")) new. Back = make. Picture(get. Media. Path("moon-surface. jpg")) chromakey(mark. P, new. Back) explore(mark. P) 03 -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 -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 -Conditionals. In. Pictures
Truth Table Conditional Operand 1 Operand 2 Result And true And true false And false false Or true Or true false true Or false Exclusive Or true false true Exclusive Or false false 03 -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 -Conditionals. In. Pictures
Challenge • Modify the general copy function to check that the target. X and target. Y 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 -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 -Conditionals. In. Pictures
- Slides: 21