 |  |

 |
 |  |  |
 | Welcome to CS1315. Click on the python to add comments.
|  |
 |  |  |
|
This page removed for FERPA compliance
|
        |
Rotating an Image by an Arbitrary Angle
#Rotating an image by an arbitrary angle
#REMOVED O'Hare (gtg377h@mail.gatech.edu)
#CS1315
#June 1, 2004
def rotate(source, target, angle):
#defining the rotation matrix that we all know and love
# [ cos(theta) sin(theta) ]
# [ -sin(theta) cos(theta) ]
rma = cos(angle)
rmb = sin(angle)
rmc = -1 * rmb
rmd = rma
# rather than actually making a 2x2 array, I jREMOVEDt made 4 variables that will
# be referenced to and I'll do the matrix operations manually
#we're going to loop through the target image
for x in range(1, getWidth(target)+1):
for y in range(1, getHeight(target)+1):
# denoting the top left pixel as (1,1) isn't real good for doing rotations
# therefore, I'm going to redefine another coordinate system where the center of
# the image is (0,0) and negative coordinates will be utilized.
xcor = x - int(getWidth(target)/2)
ycor = y - int(getHeight(target)/2)
# now I'm going to take these coordinates and rotate them REMOVEDing
# the rotational matrix. These new coordinates (sx, sy) will be
# REMOVEDed to determine which color to look for in the source picture
sx = xcor * rma + ycor * rmb + getWidth(source)/2
sy = xcor * rmc + ycor * rmd + getHeight(source)/2
# the beauty of this method is that when you loop through the
# target image, you will know that there will always be a coresponding
# color on the source image. If I were to loop through the source image
# coordinates, then I wouldn't be guaranteed a 1 to 1 corelation of pixels
# and there'll probably be an even distribution of "holes" in the final image
# if the rotated coordinates goes off the bounds of the source image,
# then ignore this loop and go on to the next one.
if ((sx < 1) or (sx > getWidth(source))) or ((sy < 1) or (sy > getHeight(source))):
pass
else:
# if not, then find the source color and plot it on the target image
color = getColor(getPixel(source, int(sx), int(sy)))
setColor(getPixel(target, x, y), color)
Before

After

Student1680
Link to this Page
- Tutorials last edited on 30 November 2006 at 7:35 pm by r40h103.res.gatech.edu