New Way of Rotating in MediaComp Python
Rich LeBlanc of Southern Catholic College sent the below code and PowerPoint slides as a better and cleaner way to talking about rotation than what's in the Python book.
10-RotatingImages.ppt
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("640x480.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
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.
#
# Set up the source and target pictures
file=getMediaPath("flower1.jpg")
picture = makePicture(file)
canvasf = getMediaPath("640x480.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
def copyClockwise():
# Like copyContercloskwise, this function
# uses the names targetX and targetY for the values that
# will be used for the X and Y values in setColor.
#
# Set up the source and target pictures
file=getMediaPath("flower1.jpg")
picture = makePicture(file)
canvasf = getMediaPath("640x480.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
# Display the results
show(picture)
show(canvas)
return canvas
def copyInverted():
# Set up the source and target pictures
file=getMediaPath("flower1.jpg")
picture = makePicture(file)
canvasf = getMediaPath("640x480.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
Links to this Page