Python Program to Rotate an Image17 Mar 2025 | 3 min read In this tutorial, we will write the Python program to solve the rotation of an image (matrix). It is a problem related to the matrix. Let's understand the problem statement. Problem StatementAn nxn 2D matrix represents an image. We need to rotate the image 90 degrees clockwise. We need to perform this operation using in-place means we do not need another 2D matrix and do the rotation. After the rotation, the image will look as below. Example -![]() Example - 1 Example - 2 Example - 3 We should follow the following constraints - SolutionWe will implement the following solution.
Let's see the following code. Example - Output: The input matrix is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] The matrix after rotation: [[7, 4, 1], [8, 5, 2], [9, 6, 3]] Explanation - In the above code, we have created a method that takes a matrix to be rotated. We iterated the outer loop, which will iterate each row of the matrix, and the outer loop is iterated over the column of each row. We changed the matrix's rows to columns and columns to rows inside the inner loop. If we print the transposed matrix, it will look as below. It will give the following output - [[1, 4, 7], [2, 5, 8], [3, 6, 9]] If we look closely at the transposed matrix, we are just one step away from getting the desired output. We only need to reverse each row. Hence we reversed each row of the matrix with the help of enumerate() function. It will give the following output - 0 [1, 4, 7] 1 [2, 5, 8] 2 [3, 6, 9] In the next line, we revered the rows and returned the matrix. Let's understand another solution. Solution - 2:In this solution, we will
Let's understand the following example. Example - Output: The input matrix is: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [[7, 8, 9], [4, 5, 6], [1, 2, 3]] The matrix after rotation: [[7, 4, 1], [8, 5, 2], [9, 6, 3]] ConclusionIn this tutorial, we have solved one of the interesting Python matrix problems that are asked by the many big organizations. We have implemented the solution using the multiple codes with the explanation. One you get the solution approach, do it your own. Next TopicValidate the IP Address in Python |
? In a classroom full of students, there is a high possibility of at least two students having the same name. How do we refer to those students? We'll use the last name or surname that identifies each person uniquely. In the Python classroom of objects, there...
10 min read
What is FTP (File Transfer Protocol)? FTP (File transfer protocol) is a network protocol for sending records between PCs over Transmission Control Protocol or protocol/internet protocol (TCP/IP) associations. Inside the TCP/IP suite, FTP is viewed as an application layer protocol. In an FTP exchange, the end...
8 min read
We just covered the most intriguing new features in this post; for a complete list of updates and changes, refer to the official release documentation. The release of Python 3.11 occurred on October 24, 2022. Python's most recent version is quicker and easier to use. It has...
15 min read
In Python, the range of float values depends on the implementation and the platform. The Python language specification only requires that floating-point numbers support at least 1e-308 to 1e+308 with a precision of at least 53 bits. In practice, most modern Python implementations use the IEEE 754...
4 min read
In this tutorial, we will learn the difference between __repr__() and __str__() methods and when these methods should be used. Displaying data to the user is one of the common tasks of the computer program. The program displays the information to the user so that the...
9 min read
__ init __ in Python If you have been using object-oriented programming, you may have run into the word "init" quite often. __init__ is a Python method. It is analogous to the constructors in languages like Java and C++. Knowing classes and objects in Python will make...
4 min read
Object Detection It is a computer vision task of classifying and identifying objects in videos or images. This object detection algorithm can be divided into two types mainly. They are: Single-shot detectors Two-shot detectors Single-shot detectors You Look only once in a single shot detector which uses full CNN to process...
4 min read
In this tutorial, we will understand what's dynamic typing in python. Whenever we write a program in python, we come across a different set of statements, one of them is an assignment statement where we initialize a variable with a value. Let us see how the assignment is...
3 min read
In the following tutorial, we will discuss a Python package called LanguageTool and understand how to create a simple grammar and spell checker using the Python programming language. So, let's get begun. Understanding the LanguageTool library in Python LanguageTool is an open-source tool used for grammar and spell-checking purposes,...
7 min read
In this tutorial, we will learn how to implement the interface in Python. Generally, the interface is not part of Python, but we can implement it using the ABC module. We will understand how to interface works and cautions of Python interface creation. Interfaces play a crucial...
7 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India