Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 112Introduction to Media Computation 1 Rotating Images.

Similar presentations


Presentation on theme: "CSC 112Introduction to Media Computation 1 Rotating Images."— Presentation transcript:

1 CSC 112Introduction to Media Computation 1 Rotating Images

2 Introduction to Media Computation2 CSC 112 Copy and Rotate def copySideways(): # This is the version from the book (Recipe 29) that # simply swithches targetX and targetY in the call to # setColor to accomplish the rotation. # # Set up the source and target pictures file=getMediaPath("flower1.jpg") picture = makePicture(file) canvasf = getMediaPath("320x240.jpg") canvas = makePicture(canvasf) # Now, do the actual copying targetX = 1 for sourceX in range(1,getWidth(picture)): targetY = 1 for sourceY in range(1,getHeight(picture)): color = getColor(getPixel(picture,sourceX,sourceY)) setColor(getPixel(canvas,targetY,targetX), color) targetY = targetY + 1 targetX = targetX + 1 # Display the results show(picture) show(canvas) return canvas

3 Introduction to Media Computation3 CSC 112 Rotating: How it works We increment the same, but we use targetX for the Y coordinate and targetY for the X coordinate

4 Introduction to Media Computation4 CSC 112 Rotating: How it works 2 We increment sourceY and targetY just the same.

5 Introduction to Media Computation5 CSC 112 Rotate: How it ends Same amount of increment, even same values in the variables, but a different result. First column of the original became the first for of the copy, and so on.

6 Introduction to Media Computation6 CSC 112 Take a closer look: The flower was reversed left-to-right as it was copied. Why? The first column of the original became the first row of the copy, when it really should be the last row to avoid reversing the image.

7 Introduction to Media Computation7 CSC 112 Copy with Counterclockwise Rotation def copyCounterclockwise(): # This version works like Recipe 30 from the book, but it # uses the names targetX and targetY for the values that # will be used for the X and Y values in setColor. file=getMediaPath("flower1.jpg") picture = makePicture(file) canvasf = getMediaPath("320x240.jpg") canvas = makePicture(canvasf) # The picture on the canvas will be filled in from bottom # to top with the columns from the original picture, as # targetY goes from getWidth(pictue) - 1 to 0 targetY = getWidth(picture) - 1 for sourceX in range(1,getWidth(picture)): targetX = 1 for sourceY in range(1,getHeight(picture)): color = getColor(getPixel(picture,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetX = targetX + 1 targetY = targetY - 1 # Display the results show(picture) show(canvas) return canvas

8 Introduction to Media Computation8 CSC 112 Other rotations We can rotate clockwise instead of counterclockwise by counting down through the values of targetX instead of targetY Inversion is accomplished by counting down through both ranges. targetX will be associated with the sourceX loop in this case, since the copied picture will be the same shape as the original.

9 Introduction to Media Computation9 CSC 112 Copy with Clockwise Rotation def copyClockwise(): # Like copyContercloskwise, this function uses the names targetX and # targetY for the values that will be used for the X and Y in setColor file=getMediaPath("flower1.jpg") picture = makePicture(file) canvasf = getMediaPath("320x240.jpg") canvas = makePicture(canvasf) # The picture on the canvas will be filled in from top # to bottom with the columns from the original picture targetY = 1 for sourceX in range(1,getWidth(picture)): # Each row of the new picture will be filled from right to left, # as targetX decreases from getHeight(picture) - 1 to 0 targetX = getHeight(picture) - 1 for sourceY in range(1,getHeight(picture)): color = getColor(getPixel(picture,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetX = targetX - 1 targetY = targetY + 1 show(picture) show(canvas) return canvas

10 Introduction to Media Computation10 CSC 112 Copy and Invert def copyInverted(): # Set up the source and target pictures file=getMediaPath("flower1.jpg") picture = makePicture(file) canvasf = getMediaPath("320x240.jpg") canvas = makePicture(canvasf) # The columns of the new picture will be added from # right to left, since targetX is counting down targetX = getWidth(picture) - 1 for sourceX in range(1,getWidth(picture)): # Each column will be filled from bottom to top, # since targetY is counting down targetY = getHeight(picture) - 1 for sourceY in range(1,getHeight(picture)): color = getColor(getPixel(picture,sourceX,sourceY)) setColor(getPixel(canvas,targetX,targetY), color) targetY = targetY - 1 targetX = targetX - 1 # Display the results show(picture) show(canvas) return canvas


Download ppt "CSC 112Introduction to Media Computation 1 Rotating Images."

Similar presentations


Ads by Google