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.

Matrix rotation

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):

    def ICV_degreeToRadian(self, degree):
        Convert degree to radian
        :param degree: degree
        :return: radian
        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
        radian = self.ICV_degreeToRadian(degree)
        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[0]):
            for img_y in range(image.shape[1]):
                # This will start with midpoints in first iteration
                # 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
                y_new = (x_prime) * math.sin(radian) + (y_prime) * math.cos(radian)

                # 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[0]) and ydist < newimage.shape[0]:
                    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.margins(0, 0)

        output_image = Image.fromarray(newImage.astype("uint8"))
        plt.savefig("output_image_rotate.jpg",bbox_inches='tight', pad_inches=0)

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

    newImage1 = rotation.ICV_rotateImageBasedOnDegree(image, 30)

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

Rotate an Image without using OpenCVrotate image

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

Leave a Reply