Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range."— Presentation transcript:

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

2 2 Announcements EXAM 1  Wednesday 09/29  6:30p - 7:30p  EE 129

3 3 ANY QUESTIONS?

4 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

5 Step 1- determine the mirror axis Step 2 - determine the flipping direction 5 1 2

6 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

7 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

8 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)

9 Recipe for vertical mirroring def mirrorVertical(source): mirrorPoint = getWidth(source) / 2 width = getWidth(source) for y in range(0,getHeight(source)): for x in range(0,mirrorPoint): leftPixel = getPixel(source,x,y) rightPixel = getPixel(source,width - x - 1,y) color = getColor(leftPixel) setColor(rightPixel,color) 9

10 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

11 Of course! 11

12 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

13 Mirroring bottom to top 13

14 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

15 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

16 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

17 Copying Barb to a canvas def 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 canvas 17

18 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

19 Copying: How it works Here’s the initial setup: 19

20 Copying: How it works 2 After incrementing the sourceY and targetY once (whether in the for or via expression): 20

21 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

22 Copying: How it looks at the end Eventually, we copy every pixel 22

23 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 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 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 26 Final QUESTIONS???


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

Similar presentations


Ads by Google