1 / 29

Python: Making colors and Using Loops

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

Download Presentation

Python: Making colors and Using Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 4 ‘a’ F(a,b) side-effects F(4, ‘a’)

  4. Example: sqrt() 4 sqrt(a) side-effects 2

  5. Example: showInformation() message showInformation(message) Pops up a new window displaying the message text

  6. Example: requestNumber() “What is the answer to Life, the Universe and Everything?” requestNumber(“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() 300 200 makeEmptyPicture(300,200) 300 x 200 blank image • makePicture(width,height) • creates and returns a picture object of the given dimensions

  8. Example: show() picture-object (an encoding of an image) show(picture-object) Pops up a new window displaying image stored in picture-object

  9. Example: pickAFile() pickAFile() Pops up a dialog box for the user to select a file Filename (eg: C:\Documents and Settings\mpapalas\My Documents\greenroom.jpg)

  10. Example: makePicture() theFile (a string containing a file name) makePicture(filename) Picture object corresponding to image that is saved in theFile • makePicture(filename) • creates and returns a picture object, from the JPEG file at the filename

  11. pickAFile() A recipe for displaying picked picture files: Pops up a dialog box for the user to select a file theFile defpickAndShow(): theFile =pickAFile()

  12. pickAFile() A recipe for displaying picked picture files: Pops up a dialog box for the user to select a file theFile makePicture(filename) defpickAndShow(): theFile =pickAFile() pic =makePicture(theFile) pic

  13. pickAFile() A recipe for displaying picked picture files: Pops up a dialog box for the user to select a file theFile makePicture(filename) defpickAndShow(): theFile =pickAFile() pic =makePicture(theFile) show(pic) pic show(picture-obj) Pops up a new window displaying image stored in the the picture object

  14. pickAFile() A recipe for displaying picked picture files: Pops up a dialog box for the user to select a file theFile makePicture(filename) defpickAndShow(): theFile =pickAFile() pic =makePicture(theFile) show(pic) pic show(picture-obj) Pops up a new window displaying image stored in the the picture object

  15. pickAFile() A recipe for displaying picked picture files: Pops up a dialog box for the user to select a file theFile makePicture(filename) pickAndShow() defpickAndShow(): theFile = pickAFile() pic = makePicture(theFile) show(pic) pic show(picture-obj) Pops up a new window displaying image stored in the the picture object

  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. red=108 green=86 blue=142 y = 9 Color:(108,86,142) Position: (12,9) x = 12

  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 How do you find out what RGB values you have? And where? JES Picture function OR Media Tools menu

  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 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”)

  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: • 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.

More Related