Week 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Week 2

Description:

pic = makePicture(pickAFile()) grayScale(pic) show(pic) ... Call your function that will modify the pic # Just as an example. nested(mypic) show(mypic) ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 25
Provided by: BillL161
Category:
Tags: pic | week

less

Transcript and Presenter's Notes

Title: Week 2


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?
Write a Comment
User Comments (0)
About PowerShow.com