import
cv2
import
numpy as np
def
SimpleWay(rotateImage, angle):
imgHeight, imgWidth
=
rotateImage.shape[
0
], rotateImage.shape[
1
]
centreY, centreX
=
imgHeight
/
/
2
, imgWidth
/
/
2
rotationMatrix
=
cv2.getRotationMatrix2D((centreY, centreX), angle,
1.0
)
rotatingimage
=
cv2.warpAffine(
rotateImage, rotationMatrix, (imgWidth, imgHeight))
return
rotatingimage
def
ModifiedWay(rotateImage, angle):
imgHeight, imgWidth
=
rotateImage.shape[
0
], rotateImage.shape[
1
]
centreY, centreX
=
imgHeight
/
/
2
, imgWidth
/
/
2
rotationMatrix
=
cv2.getRotationMatrix2D((centreY, centreX), angle,
1.0
)
cosofRotationMatrix
=
np.
abs
(rotationMatrix[
0
][
0
])
sinofRotationMatrix
=
np.
abs
(rotationMatrix[
0
][
1
])
newImageHeight
=
int
((imgHeight
*
sinofRotationMatrix)
+
(imgWidth
*
cosofRotationMatrix))
newImageWidth
=
int
((imgHeight
*
cosofRotationMatrix)
+
(imgWidth
*
sinofRotationMatrix))
rotationMatrix[
0
][
2
]
+
=
(newImageWidth
/
2
)
-
centreX
rotationMatrix[
1
][
2
]
+
=
(newImageHeight
/
2
)
-
centreY
rotatingimage
=
cv2.warpAffine(
rotateImage, rotationMatrix, (newImageWidth, newImageHeight))
return
rotatingimage
DogImage
=
cv2.imread(
"doggy.png"
,
1
)
NormalRotation
=
SimpleWay(DogImage,
40
)
ModifiedVersionRotation
=
ModifiedWay(DogImage,
40
)
cv2.imshow(
"Original Image"
, DogImage)
cv2.imshow(
"Normal Rotation"
, NormalRotation)
cv2.imshow(
"Modified Version Rotation"
, ModifiedVersionRotation)
cv2.waitKey(
0
)
cv2.destroyAllWindows()