+ All Categories
Home > Documents > 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and...

1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and...

Date post: 04-Jan-2016
Category:
Upload: aubrey-beasley
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
25
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range
Transcript
Page 1: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

1

CS 177 Week 5 Recitation Slides

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

Page 2: 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

Page 3: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

3

ANY QUESTIONS?

Page 4: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 5: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

Step 1- determine the mirror axis

Step 2 - determine the flipping direction

5

1

2

Page 6: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 7: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 8: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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)

Page 9: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 10: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

Of course!

11

Page 11: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 12: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

Mirroring bottom to top

13

Page 13: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 14: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 15: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 16: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 17: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 18: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

Copying: How it works

Here’s the initial setup:

19

Page 19: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

Copying: How it works 2

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

20

Page 20: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 21: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

Copying: How it looks at the end

Eventually, we copy every pixel

22

Page 22: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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.

Page 23: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 24: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

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

Page 25: 1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.

26

Final QUESTIONS???


Recommended