1 cs 177 week 5 recitation slides mirroring and copying images, using for loop, if statement, and...

Post on 04-Jan-2016

213 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

CS 177 Week 5 Recitation Slides

Mirroring and copying images,Using for Loop, if statement, and range

2

Announcements EXAM 1

Wednesday 09/29 6:30p - 7:30p EE 129

3

ANY QUESTIONS?

Horizontal mirror recipe mirroring means intuitively "flipping around" an axis (when

you mirror horizontally, you flip your picture around a vertical axis)

STEP 1. Since the picture is represented by a matrix, you must determine the coordinates (x and y) of all the "points" of this axis in the matrix

STEP 2. Then you have to determine the direction of the flipping (when you mirror horizontally, you may flip the left side to right side or vice versa)

STEP3. Now, since pictures are encoded as a matrices, you must figure out where a pixel of the source picture should go in the target picture

4

Step 1- determine the mirror axis

Step 2 - determine the flipping direction

5

1

2

Work it out with matrices To find out the mirror axis you need just to determine its x

coordinate (the mirrorPoint). It is is halfway across: getWidth(picture)/2

6

Work it out with matrices STEP 2. If the flipping direction is left to right, then the

source and target matrices will look like this:

7

Step 3 Figure out where a pixel of the source picture should go

in the target picture

8

If source pixel is at (x,y), target pixel is at (width-x-1,y)

Can we do it with a horizontal mirror?

def mirrorHorizontal(source): mirrorPoint = getHeight(source) / 2 height = getHeight(source) for x in range(0,getWidth(source)): for y in range(0,mirrorPoint): topPixel = getPixel(source,x,y) bottomPixel = getPixel(source,x,height - y - 1) color = getColor(topPixel) setColor(bottomPixel,color)

10

Of course!

11

What if we wanted to copy bottom to top?

Very simple: Swap the order of pixels in the bottom lines

def mirrorBotTop(source): mirrorPoint = getHeight(source) / 2 height = getHeight(source) for x in range(0,getWidth(source)): for y in range(0,mirrorPoint): topPixel = getPixel(source,x,y) bottomPixel = getPixel(source,x,height - y - 1) color = getColor(bottomPixel) setColor(topPixel,color)

12

Mirroring bottom to top

13

Some Utility Functions

If you know the name of the file, searching for it with pickAFile() feels tedious

You can set and get a media folder (path) for remembering a place where your media will be coming from (or going to) setMediaPath() lets you pick a file in your media folder getMediaPath(basefilename) lets you generate a complete

filename out of only the last part

14

Utility functions example

>>> setMediaPath()New media folder: C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\>>> getMediaPath("barbara.jpg")'C:\\Documents and Settings\\Mark Guzdial\\My Documents\\mediasources\\barbara.jpg'>>> barb=makePicture(getMediaPath("barbara.jpg"))

15

Copying pixels

In general, what we have to do is to keep track of the source index variables (sourceX and sourceY), and of the target index variables (targetX and targetY). We increment (add to them) in pairs

sourceX and targetX get incremented together sourceY and targetY get incremented together

The tricky parts are: Setting values inside the body of loops Incrementing at the bottom of loops

16

Copying Barb to a canvasdef copyBarb(): # Set up the source and target pictures barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying targetX = 0 for sourceX in range(0,getWidth(barb)): targetY = 0 for sourceY in range(0,getHeight(barb)): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetY = targetY + 1 targetX = targetX + 1 show(barb) show(canvas) return canvas17

Copying into the middle of the canvas

def copyBarbMidway(): # Set up the source and target pictures barbf=getMediaPath("barbara.jpg") barb = makePicture(barbf) canvasf = getMediaPath("7inX95in.jpg") canvas = makePicture(canvasf) # Now, do the actual copying targetX = 100 for sourceX in range(0,getWidth(barb)): targetY = 100 for sourceY in range(0,getHeight(barb)): color = getColor(getPixel(barb,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetY = targetY + 1 targetX = targetX + 1 show(barb) show(canvas) return canvas

18

Copying: How it works

Here’s the initial setup:

19

Copying: How it works 2

After incrementing the sourceY and targetY once (whether in the for or via expression):

20

Copying: How it works 3

After yet another increment of sourceY and targetY:

When we finish that column, we increment sourceX and targetX, and start on the next column.

21

Copying: How it looks at the end

Eventually, we copy every pixel

22

23

Functions with return values Useful when we need to access the output generated by a function, not just

print the value. The following command is used to return the value of a variable abc that is

generated inside a function.

return abc The return statement lets you assign the output of a function to another

variable so that it can be used later.

The following slides explain what this means.

24

Functions with return values (contd)

The above function prints the value of c on the screen

The function below returns the value of c to the place where it was called (i.e. the command window). The effect is the same as having a print statement instead of a return

25

Functions with return values (contd) The following examples explain the difference

between printing a value within the function and returning the value from a function

26

Final QUESTIONS???

top related