Download presentation
Presentation is loading. Please wait.
Published byPercival Brooks Modified over 9 years ago
1
Python: Making colors and Using Loops
2
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 JES Functions for picture manipulation JES-->Pictures
3
Review: Functions in general F(a,b) 4‘a’ F(4, ‘a’) side-effects
4
Example: sqrt() sqrt(a) 4 2 side-effects
5
Example: showInformation() showInformation(message) message Pops up a new window displaying the message text
6
Example: requestNumber() requestNumber(“What is the answer to Life, the Universe and Everything?” ) “What is the answer to Life, the Universe and Everything?” Pops up a new window displaying the message text with an input box where the user can type in a number, e.g., 42 42
7
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
8
Example: show() show(picture-object) picture-object (an encoding of an image) Pops up a new window displaying image stored in picture-object
9
Example: pickAFile() pickAFile() Filename (eg: C:\Documents and Settings\mpapalas\My Documents\greenroom.jpg) Pops up a dialog box for the user to select a file
10
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
11
pickAFile() Pops up a dialog box for the user to select a file theFile A recipe for displaying picked picture files: def pickAndShow(): theFile = pickAFile()
12
pickAFile() Pops up a dialog box 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:
13
pickAFile() Pops up a dialog box 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:
14
pickAFile() Pops up a dialog box 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:
15
pickAFile() Pops up a dialog box 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) pickAndShow() A recipe for displaying picked picture files:
16
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.
17
Reminder: Common errors >>> file = pickAFile() >>> pic = makePicture(file) >>> show(file) The error was:sample Name 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 pic Picture, filename C:\Documents and Settings\mpapalas\Desktop\sample.jpg height 1200 width 1600 Oops!
18
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
19
Color:(108,86,142) Position: (12,9) x = 12 y = 9 red=108green=86blue=142
20
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)
21
Media Tools JES Picture function OR Media Tools menu How do you find out what RGB values you have? And where?
22
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 file path: the path to the file you want the picture written to Takes 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”)
23
Conditionals if age < 18: showInformation(“Sorry, not allowed to vote yet.”) else: showInformation(“Please select candidate.”)
24
Repetition for number in range(0,N): pic =.... filename = base + str(number) writePictureTo(pic, filename)
25
Lists [“hi”, “hello”, “howdy”, “hiya”, “yo”] [10, 23, 15] [] range(0,6) range(1,4) range(3,8)
26
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.
27
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.
28
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.
29
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.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.