240 likes | 386 Views
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?.
E N D
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? • Basics of writing a computer program in Jython using JES • Names • Quantities (Numbers) • Things (Pictures, strings) • Functions • Files
Have you actually written a computer program? • Yes?/No? • Why doesn't it look like a computer program?
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()
Can a function that you write call a function that you write?
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?
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"
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!
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)
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)
Selective Manipulation (x1,y1) (x2,y2)
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!
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)
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!!!
Translating • Can we put a picture wherever we want it?
Another Useful Tool makeEmptyPicture(width, height) Can you guess what it does? Can you guess what it returns?
Idea • Let's make up our own "picture" from scratch!
Where? (xpos, ypos)
Scaling • Can we make pictures bigger or smaller? • Can we stretch them?