python: working with pixels. reminder: conditionals if age < 18: showinformation(“sorry, not...

20

Click here to load reader

Upload: brianna-warner

Post on 19-Jan-2018

212 views

Category:

Documents


0 download

DESCRIPTION

Relational Operators a > b a < b a == b (equality) a = b (“greater than or equal”) != (inequality) Note: The order of signs matters: =! = do not work! There should be no spaces inside the relational operator, so “a < = b” also does not work (See also Appendix A.4, p362)

TRANSCRIPT

Page 1: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Python: Working with pixels

Page 2: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Reminder: Conditionalsif age < 18: showInformation(“Sorry, not allowed to vote yet.”)else: showInformation(“Please select candidate.”)

Page 3: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Relational Operators

• a > b• a < b• a == b (equality)• a <= b (“less than or equal”)• a >= b (“greater than or equal”) • != (inequality) Note: The order of signs matters: =! =< => do not work!There should be no spaces inside the relational operator, so “a < = b” also

does not work (See also Appendix A.4, p362)

Page 4: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Getting the leading zero into the filename:

if n < 10: filename = “swatch0” + str(n) + “.jpg”else: filename = “swatch” + str(n) + “.jpg”

Page 5: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Getting the leading zero into the filename:

if n < 10: filename = “swatch0” + str(n) + “.jpg”else: # here we know n >= 10 filename = “swatch” + str(n) + “.jpg”

What if you need more frames?if n < 10: filename = “swatch00” + str(n) + “.jpg”else: # here we know n >= 10 if n < 100: # here n is in the range of 10 … 99 filename = “swatch0” + str(n) + “.jpg” else: # here we know n >= 100 filename = “swatch” + str(n) + “.jpg”

Page 6: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Examples from lab 11

• Be sure to review these!• Open examples.py in JES• Load• In the command area, try the functions

• Here are links to small images you can use to test your program (or use your own):  eye.jpg, luca.jpg

Page 7: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Getting at the pixels

getPixel(picture,x,y)

Gets a single pixel – returns pixel at position x,y of picture

getPixels(picture)

gets all the pixels – returns an array containing all the pixels of picture

Page 8: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Reminder: Manipulating Pictures

>>> pic1 = makeEmptyPicture(200,100)>>> seafoam = makeColor(153, 255, 204)>>> setAllPixelsToAColor(pic1, seafoam)>>> addText(pic1,30,50,“hello")>>> show(pic1)>>> pic2 = makePicture(pickAFile())>>> show(pic2)

Page 9: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Color:(108,86,142) Position: (12,9)

x = 12

y = 9 red=108 green=86 blue=142

>>> p = getPixel(picture,12,9)>>> print pPixel, red=108 green=86 blue=142

Page 10: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

What can we do with a pixel p?

• getRed(p), • getGreen(p)• getBlue(p)

• setRed(p, value)• setGreen(p, value)• setBlue(p, value)

functions that take a pixel (p) as input and return a value between 0 and 255

functions that set the value of pixel (p) to a given value between 0 and 255

Page 11: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

We can also get, set, and modify Colors

• getColor(p) takes a pixel as input and returns a Color object with the color at that pixel

• setColor(p, c) sets the color of pixel (p) as input and a color (c), then sets the pixel to that color.

• We also have functions that can makeLighter(c) and makeDarker(c) an input color

• Last time we saw that we can also create colors:– makeColor(r,g,b) takes red, green, and blue values (in

that order) between 0 and 255, and returns a Color object– pickAColor() lets you use a color chooser and returns

the chosen color

Page 12: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Color:(108,86,142) Position: (12,9)

x = 12

y = 9 red=108 green=86 blue=142

>>> pixel=getPixel(picture,12,9)>>> print pixelPixel, red=108 green=86 blue=142>>> value = getRed(pixel)>>> setRed (pixel, value+50)>>> setGreen(pixel, 0)>>> setBlue(pixel, getBlue(pixel)/2)

Page 13: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Color:(108,86,142) Position: (12,9)

x = 12

y = 9 red=108 green=86 blue=142

>>> pixel=getPixel(picture,12,9)>>> print pixelPixel, red=108 green=86 blue=142>>> value = getRed(pixel)>>> setRed (pixel, value+50)>>> setGreen(pixel, 0)>>> setBlue(pixel, getBlue(pixel)/2)

Color:(158,0,121) Position: (12,9)

y = 9 red=158 Green=0 Blue=121

Page 14: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Color:(108,86,142) Position: (12,9)

x = 12

y = 9 red=108 green=86 blue=142

>>> pixel=getPixel(picture,12,9)>>> print pixelPixel, red=108 green=86 blue=142>>> redValue = getRed(pixel)>>> greenValue = getGreen(pixel)>>> blueValue = getBlue(pixel)>>> newColor = makeColor(redValue+50, 0, getBlue(pixel)/2)>>> setColor(pixel, newColor)

Page 15: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Color:(158,0,121) Position: (12,9)

x = 12

y = 9 red=158 Green=0 Blue=121

>>> pixel=getPixel(picture,12,9)>>> print pixelPixel, red=108 green=86 blue=142>>> redValue = getRed(pixel)>>> greenValue = getGreen(pixel)>>> blueValue = getBlue(pixel)>>> newColor = makeColor(redValue+50, 0, getBlue(pixel)/2)>>> setColor(pixel, newColor)

Page 16: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Repeating an action for all the pixels in a picture

Example:

for p in getPixels(picture):    value = getRed(p)    setRed(p, value*0.5)

Page 17: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Repeating an action for all the pixels in a picturedecreaseRed()

Example:def decreaseRed(picture): for p in getPixels(picture):    value = getRed(p)    setRed(p, value*0.5)

Page 18: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

More examples:

More examples (link)   - you can copy and paste these to save time

• decreaseGreen()• decreaseBlue()• clearBlue()• lighten()• darken()• negative() • grayScale()

18

Page 19: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

“Posterize” function•For each pixel, if red<128, we set red=0, otherwise we set red=255•Similarly for green, blue

Page 20: Python: Working with pixels. Reminder: Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select

Assignment: Create a python function for each of the following:

1. Decrease red by 20%2. Decrease green by 20%3. Decrease blue by 20%4. Increase red by 20%, if possible (i.e., if it does not

exceed 255)5. Similarly for increasing blue and green6. “Posterize” 7. Think of another way to change an image and

implement a function to do that

20