python: modifying pictures using loops. review jes command area – program area defining/using...

24
Python: Modifying Pictures Using Loops

Upload: kya-woodhams

Post on 14-Jan-2016

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Python: Modifying Pictures Using Loops

Page 2: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Review

• JES command area – program area

• Defining/using functionsspecifying a sequence of steps for what the function should do.

• JES Functions for file manipulation JES->Files

• JES Functions for interacting with user JES-->Input/Output

• JES Functions for picture manipulation JES-->Pictures

Page 3: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Reminder: Manipulating Pictures

>>> pic1 = makeEmptyPicture(200,100)

>>> show(pic1)

>>> setAllPixelsToAColor(pic1, red)

>>> show(pic1)

>>> addText(pic1,30,50,“hello")

• similarly can add rectangles, lines, etc.

Page 4: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Reminder: Common errors

>>> file = pickAFile()>>> pic = makePicture(file)>>> show(file)The error was:sampleName not found globally.A local or global name could not be found. You need to define the function or variable before you try to use it in any way.>>> show(pic)>>> print picPicture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600

Oops!

Page 5: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Making new Colors• Some names for common colors are

predefined – try using the names: yellow, black, white, etc.

• makeColor() 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 6: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

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

x = 12

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

Page 7: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Encoding RGB

Colors go from (0,0,0) to (255,255,255)

>>> pic2 = makeEmptyPicture(200,100)>>> show(pic2)>>> seafoam = makeColor(153, 255, 204)>>> setAllPixelsToAColor(pic2, seafoam)>>> show(pic2)

Page 8: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Manipulating pixels

getPixel(picture,x,y)

gets a single pixel.

getPixels(picture)

gets all of them in an array

Page 9: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

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>>> allThePixels=getPixels(picture)>>> print allThePixels[0]Pixel red=191 green=149 blue=120

Page 10: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

What can we do with a pixel?

• getRed(), getGreen(), and getBlue() are functions that take a pixel as input and return a value between 0 and 255

• setRed(), setGreen(), and setBlue() are functions that take a pixel as input and a value between 0 and 255

Page 11: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

We can also get, set, and modify Colors

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

• setColor() takes a pixel as input and a Color, then sets the pixel to that color

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

• Last time we saw that we can also create colors:– makeColor() 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: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Demonstrating: Manipulating Colors

>>> print getRed(pixel)168>>> setRed(pixel,255)>>> print getRed(pixel)255>>> color=getColor(pixel)>>> print colorcolor r=255 g=131 b=105>>> setColor(pixel,color)>>> newColor=makeColor(0,100,0)>>> print newColorcolor r=0 g=100 b=0>>> setColor(pixel,newColor)>>> print getColor(pixel)color r=0 g=100 b=0

>>> print colorcolor r=81 g=63 b=51>>> print newcolorcolor r=255 g=51 b=51>>> print colorcolor r=168 g=131 b=105>>> print makeDarker(color)color r=117 g=91 b=73>>> print colorcolor r=117 g=91 b=73>>> newcolor=pickAColor()>>> print newcolorcolor r=255 g=51 b=51

Page 13: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

We can change pixels directly…

>>> file= pickAFile()>>> pict=makePicture(file)>>> show(pict)>>> setColor(getPixel(pict,10,100),yellow)>>> setColor(getPixel(pict,11,100),yellow)>>> setColor(getPixel(pict,12,100),yellow)>>> setColor(getPixel(pict,13,100),yellow)>>> repaint(pict)

Page 14: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Media Tools

JES Picture function

OR

Media Tools menu

How do you find out what RGB values you have? And where?

Page 15: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

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 16: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

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 17: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

More examples:

• decreaseGreen()

• decreaseBlue()

• clearBlue()

• lighten()

• darken()

• negative()

• grayScale()

Page 18: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Saving to a file

• setMediaPath():Prompts the user to pick a folder on the computer. JES then will look for files in that directory unless given a full path, i.e. one that starts with "c:\"

• writePictureTo(picture, path):picture: the picture you want to be written out to a filepath: the path to the file you want the picture written toTakes a picture and a file name (string) as input, then writes the picture to the file as a JPEG.

• Example: writePictureTo(pic, “mypic.jpg”)

Page 19: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Assignment

Create picture manipulation functions that do the following:

1. Decrease red by 10%2. Decrease green by 10%3. Decrease blue by 10%4. Make the picture darker by decreasing red, green,

and blue by 10%5. Make a grayscale version of the image and then

negate it (B/W negative)

Page 20: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Next time:

• Repetition

• Conditionals

• Lists

• Menu-driven program

Page 21: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Repetition

for number in range(0,N):

pic = ....

filename = “pic” + str(number)

writePictureTo(pic, filename)

Page 22: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Conditionals

if age < 18:

showInformation(“Sorry, not allowed to vote yet.”)

else:

showInformation(“Please select candidate.”)

Page 23: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Lists

• [“hi”, “hello”, “howdy”, “hiya”, “yo”]

• [10, 23, 15]

• []

• range(0,6)

• range(1,4)

• range(3,8)

Page 24: Python: Modifying Pictures Using Loops. Review JES command area – program area Defining/using functions specifying a sequence of steps for what the function

Preview of next assignmentCreate a menu-driven picture manipulation program that allows the user to make

various changes to images. Here is the algorithm:• welcome the user to your program • get the user to select a picture to work on • show(picture) • ask the user to select an option from a list of numbers:

– reduce red – reduce green – reduce blue – posterize – grayscale – <a feature of your choice>

• repaint(picture) • ask user whether to save the picture work, and if the response is “yes”, save it • show goodbye/thank you message