Programming with Pictures

advertisement
Computer Science 101
Introduction to
Programming with Pictures
setMediaPath

Invoking this function allows the user to choose
a folder that will be the “default” location where
searches for media files will start. This just
makes it easier to pick files, etc.
JES Picture Layout

With JES, pictures are laid out in an x,y
coordinate system starting in upper left corner,
with x increasing to the right and y increasing
downward.
Working with Pixels – get values
getPixel(picture,x,y) – obtains the pixel at
the given coordinates in the given picture.
 getColor(pixel) – obtains the color object
of the pixel.
 getRed(pixel) – obtains the red value of
the pixel, etc.
 getX(pixel) – obtains the X coordinate of
the pixel, etc.

Working with Pixels – changing
values

setColor(pixel, color) – changes the pixel’s
color to be the given color.

setRed(pixel, redAmt) – changes the
pixel’s red value to the given amount, etc.

pickAColor() – lets the user choose a color
with slide bars, etc.
ZOOM
List of Pixels
getPixels(myPicture) - returns a list of all the
pixels from the picture.
 The pixels will be in the list in row order – first
row followed by second row, etc.


Note that the pixel in position
(x,y) has list index equal to
width * y + x
For example, the pixel at (3,2)
has index 2*4 + 3
Changing Individual Pixels
1.
Get the pixel into a variable:
myPixel = getPixel(myPicture,10,6)
2.
If the change does not depend on current
values in the pixel, make change:
setRed(myPixel, 128)
3.
If change depends on current values, get the
needed values first:
myRed = getRed(myPixel)
setRed(myPixel, myRed * 0.5)
Processing all the Pixels

The general strategy is to use a for-loop on the
list of pixels:
thePixels = getPixels(thePicture)
for aPixel in thePixels :
<process aPixel>
or
for aPixel in getPixels(thePicture) :
<process aPixel>
Choosing Pixels to Process

Sometimes we decide which pixels to process or
which process to apply depending on conditions
on the pixels
thePixels = getPixels(thePicture)
for aPixel in thePixels:
if <condition about aPixel>
<process aPixel>
Working with Pixels within a Region

Often we can restrict our processing to pixels
within some rectangular region of the picture.
This can be much, much more efficient than
iterating over all of the multitude of pixels.
for x in range(a,b) :
for y in range(c,d) :
<process pixel at x,y>
Parameter for Color
Add Parameters for Width, Height,
and Starting Point
Negative
Lighten
GrayScale
Sepia Tint
Rotate 90
Copy Rectangle
Edge Detection
Blend
Frame
Download