1 / 24

Week 2

Week 2. Homework. Are you collaborating effectively? Are you starting early? Are you running your homework code to see if it works? Homework 2 is now posted. Due this coming Friday night at midnite!. Prequiz. Are you collaborating effectively?. What have we learned so far?.

tova
Download Presentation

Week 2

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. Week 2

  2. Homework • Are you collaborating effectively? • Are you starting early? • Are you running your homework code to see if it works? • Homework 2 is now posted. • Due this coming Friday night at midnite!

  3. Prequiz • Are you collaborating effectively?

  4. What have we learned so far? • Basics of writing a computer program in Jython using JES • Names • Quantities (Numbers) • Things (Pictures, strings) • Functions • Files

  5. Have you actually written a computer program? • Yes?/No? • Why doesn't it look like a computer program?

  6. JES commands • def • pickAFile() • picture = makePicture(filename) • thePixels = getPixels(picture) • getRed(pixel), getGreen(pixel), getBlue(pixel) • setRed(pix, col), setGreen..., setBlue... • color = makeColor(r, g, b) • setColor(pixel, color) • show(picture) • showVars()

  7. Can a function call a function?

  8. Can a function that you write call a function that you write?

  9. Example def grayScale(picture): for px in getPixels(picture): r = getRed(px) * 0.299 g = getGreen(px) * 0.587 b = getBlue(px) * 0.114 lum = r + g + b setColor(px, makeColor(lum, lum, lum) def grayPicker(): pic = makePicture(pickAFile()) grayScale(pic) show(pic) # Should this function return something?

  10. New Material

  11. MediaPath • As a convenience, Jython contains a name called MediaPath. You don't use this name directly. Instead you use two functions: • setMediaPath() • Opens a file chooser window • Allows you to choose a directory! • Sets MediaPath to the value of that directory path which is a string • getMediaPath("filename") • returns the value of MediaPath concatenated with the "filename"

  12. range() • The "for loop" in JES allows you to create a loop structure and have a "loop variable" take on all of the different value in a collection for px in getPixels(picture): • You can even make up a collection: for loopvar in [1, 3, "apple", 7000] • But sometimes you just want to have a loop run with the loop variable having an increasing (or decreasing) sequence of integers • Enter the range function!

  13. Individual Pixel Manipulation • So far we have seen operations on a whole collection of pixels ( i.e. getPixels(picture) ) • But sometimes we would like to be able to be more selective in out manipulation of pixels pixel = getPixel(picture, x, y)

  14. Picture Size • With getPixels we basically had a collection of pixels we could go through one at a time • With getPixel things get a little more complex. Now we have to specifiy the x and y of the pixel. • How do we even know the size of a picture? • width = getWidth(picture) • height = getHeight(picture)

  15. Nested for Loops

  16. Selective Manipulation (x1,y1) (x2,y2)

  17. Nested for Loops def nested(pict): for x in range(1, getWidth(pict)+1): for y in range(1, getHeight(pict)+1): pix = getPixel(pict, x, y) # Do whatever processing you want # to the pixel here!

  18. Homework 2 Template def hw2(): mypic = makePicture(getMediaPath('myPicFile.jpg')) # Call your function that will modify the pic # Just as an example nested(mypic) show(mypic)

  19. Homework 2 Checklist • Write a function (or functions) that will manipulate a picture. • Make sure that your program uses getMediaPath • Put the program and file in the same folder • Using WebWork turn BOTH files in as hw2 • Make sure to retrieve the submission and run it just like the grader will run it!!!

  20. Translating • Can we put a picture wherever we want it?

  21. Another Useful Tool makeEmptyPicture(width, height) Can you guess what it does? Can you guess what it returns?

  22. Idea • Let's make up our own "picture" from scratch!

  23. Where? (xpos, ypos)

  24. Scaling • Can we make pictures bigger or smaller? • Can we stretch them?

More Related