29
Python: Making colors and Using Loops

Python: Making colors and Using Loops

Embed Size (px)

DESCRIPTION

Python: Making colors and Using Loops. Review. JES command area – program area Defining/using functions specifying 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 - PowerPoint PPT Presentation

Citation preview

Python: Making colors and Using Loops

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

Review: Functions in general

F(a,b)

4 ‘a’

F(4, ‘a’)

side-effects

Example: sqrt()

sqrt(a)

4

2

side-effects

Example: showInformation()

showInformation(message)

message

Pops up a new window displaying the message text

Example: requestNumber()

requestNumber(“What is the answer to Life, the Universe

and Everything?” )

“What is the answerto Life, the Universe

and Everything?”

Pops up a new windowdisplaying the message text

with an input box where the usercan type in a number, e.g., 42

42

Example: makeEmptyPicture()

makeEmptyPicture(300,200)

300 x 200 blank image

300

makePicture(width,height)• creates and returns a picture object of the given dimensions

200

Example: show()

show(picture-object)

picture-object(an encoding of an image)

Pops up a new window displaying image stored

in picture-object

Example: pickAFile()

pickAFile()

Filename (eg: C:\Documents and Settings\mpapalas\My Documents\greenroom.jpg)

Pops up a dialogbox for the user to

select a file

Example: makePicture()

makePicture(filename)

Picture object corresponding to image that is saved in theFile

theFile (a string containing a file name)

makePicture(filename)• creates and returns a picture object, from the JPEG file at the filename

pickAFile() Pops up a dialogbox for the user to

select a file

theFile

A recipe for displaying

picked picture files:

def pickAndShow(): theFile = pickAFile()

pickAFile() Pops up a dialogbox for the user to

select a file

makePicture(filename)

pic

theFile

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile)

A recipe for displaying

picked picture files:

pickAFile() Pops up a dialogbox for the user to

select a file

makePicture(filename)

pic

theFile

show(picture-obj)Pops up a new window displaying image stored in the the picture object

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)

A recipe for displaying

picked picture files:

pickAFile() Pops up a dialogbox for the user to

select a file

makePicture(filename)

pic

theFile

show(picture-obj)Pops up a new window displaying image stored in the the picture object

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic)

A recipe for displaying

picked picture files:

pickAFile() Pops up a dialogbox for the user to

select a file

makePicture(filename)

pic

theFile

show(picture-obj)Pops up a new window displaying image stored in the the picture object

def pickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic) pi

ckAnd

Show

()

A recipe for displaying

picked picture files:

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.

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!

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

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

x = 12

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

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)

Media Tools

JES Picture function

OR

Media Tools menu

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

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: >>> setMediaPath() writePictureTo(pic, “mypic.jpg”)

Conditionals

if age < 18:

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

else:

showInformation(“Please select candidate.”)

Repetition

for number in range(0,N):

pic = ....

filename = base + str(number)

writePictureTo(pic, filename)

Lists

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

• [10, 23, 15]

• []

• range(0,6)

• range(1,4)

• range(3,8)

Assignment

Step 1:Create a function makeMeSwatches() that makes lots

of color swatches (small solid-colored image files). Specifically, your function should:– display a welcome message to the user– ask the user to say how many swatches – input

that and save in a variable– create small files with images of solid color save

each one with names "swatch00.jpg", "swatch01.jpg", "swatch02.jpg", etc (do this using a loop)

– display a message letting the user know that the program is complete and swatches are saved in the default folder.

Assignment

Step 2:To test your function:1) from the command line, ask the user to select a

folder for storing the swatches, using setMediaFolder() – you may have already done this during this session, so you only need to repeat it if you want to change the default folder for saving the files (and each time you start a new JES session).

2) invoke your function by using makeMeSwatches() from the command line. Try 10 for the number of swatches, and when it finishes running, check the folder you selected to make sure the files were created.

Assignment

Step 3:

• Improve your function by adding a rectangle or other shape that moves to a different position with each frame.

• Be sure to test your function well, each time you make some changes.

Assignment Step 4:• Make sure you have a folder with at least 100 images,

named swatch00.jpg, swatch01.jpg, etc (or some other systematic name).

• In the command area, create a movie: swatchesMovie = makeMovieFromInitialFile("swatch00.jpg")

• Now use playMovie(swatchesMovie) to view your movie. You can adjust the number of frames/sec and view the movie or individual frames

• If you are satisfied with it, click Write Quicktime or Write AVI to save the movie.