# Rotate an Image Without Using OpenCV

## Rotate an Image without using OpenCV

In this blog, you will find the steps to rotate an image without using the OpenCV library. However, rotating an image using a library like OpenCV is very very easy. But the objective of this blog is to explain the traditional way of rotating an image and the underlying mathematics behind the rotation.

Below is the formula to transform an image from (x,y) to a different angel. Below are few steps to consider:

• Step1: The first step is to convert degree to radian as NumPy takes radian as input.
• Step2: Defined new Image(canvas) size which is dynamic. As the new image dimension will change as part of the rotation process.
• Step3: Start applying logic to rotate from the image midpoint.
• Once, the image mid-point is identified, start applying rotation logic
• Adjust the new rotation point with respect to the canvas image.
• Check if the new points do not cross the defined canvas size (row and columns)
• Step4: Plot images
```import numpy as np
import math
import pylab
from PIL import Image
import matplotlib.pyplot as plt

class ICV_RotateImage:
#Rotate an Image without using OpenCV
def __init__(self):
pass

'''
:param degree: degree
'''
return (degree * np.pi) / 180.0

def ICV_getImageMidPoints(self, image):
'''
This method returns image midpoints
:param image:
:return: x_mid, y_mid
'''
height, width, num_channels = image.shape
y_mid = height / 2
x_mid = width / 2
return x_mid, y_mid

'''
rotate image in python
'''
def ICV_rotateImageBasedOnDegree(self, image, degree):
'''
Rotate image based on degree
:param image: actual image
:param degree: degree
:return: rotated image
'''
x_mid, y_mid = self.ICV_getImageMidPoints(image)

newimage = self.ICV_canvasSize(image)

newimage_x_mid, newimage_y_mid = self.ICV_getImageMidPoints(newimage)
print("New image midpoint:newimage_x_mid",newimage_x_mid ,"newimage_y_mid" , newimage_y_mid )

for img_x in range(image.shape):
for img_y in range(image.shape):
# and then propagates back
y_prime = y_mid - img_y
x_prime = x_mid - img_x

x_new = (x_prime) * math.cos(radian) - (y_prime) * math.sin(radian) #Rotate an Image without using OpenCV

# Adjust each new points wrt newImage mid points
xdist = int(newimage_x_mid - x_new)
ydist = int(newimage_y_mid - y_new)
if (xdist < newimage.shape) and ydist < newimage.shape:
newimage[xdist][ydist][:] = image[img_x][img_y][:]

return newimage

def ICV_canvasSize(self, image):
'''
This method generates Canvas Size based on Input Image
:param image: image array
:return: canvas
'''
height, width, num_channels = image.shape
max_len = int(math.sqrt(width * width + width * width))
canvas = np.zeros((max_len, max_len, 3))
return canvas

def ICV_plotImage(self, newImage):
'''
This method plots the new image
:param newImage:
:return: Null
'''
plt.gca().set_axis_off()
plt.margins(0, 0)
plt.gca().xaxis.set_major_locator(plt.NullLocator())
plt.gca().yaxis.set_major_locator(plt.NullLocator())

output_image = Image.fromarray(newImage.astype("uint8"))
plt.imshow(output_image)
plt.show()

def main():
image = Image.open('image1.jpg')
image = np.asarray(image)
print(image.shape)
rotation = ICV_RotateImage() ## Create an Object

newImage1 = rotation.ICV_rotateImageBasedOnDegree(image, 30)
rotation.ICV_plotImage(newImage1)

#main method to Rotate an Image without using OpenCV
if __name__ == "__main__":
main()

```

Output:  In this blog, you learned about how to rotate an image without using OpenCV, and follow the code to rotate images in python using python. Please comment below in case of any doubts.

#rotate images in python