View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image
module provides a class with
the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, and to create new images.
Image.transform[]
Transforms this image. This method creates a new image with the given size, and the same mode as the original, and copies data to the new image using the given transform.
Syntax: Image.transform[size, method, data=None, resample=0, fill=1]
Parameters:
size – The output size.
method – The transformation method.
data – Extra data to the transformation method.
resample – Optional resampling filter.Returns: An Image object.
Image Used:
from
PIL
import
Image
img
=
Image.
open
[r
"C:\Users\System-Pc\Desktop\tree.jpg"
]
img1
=
img.transform[[
300
,
300
], Image.EXTENT,
data
=
[
10
,
0
,
10
+
img.width
/
/
4
, img.height
/
/
3
]]
img1.show[]
Output:
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
In this article, let’s see how to rotate an Image using Python. By Image Rotation, the image is rotated about its center by a specified number of degrees. The rotation of an image is a geometric transformation. It can be done either by Forward Transformation [or] Inverse Transformation.
Here Image Processing Library with Pillow uses Inverse Transformation. If the Number Of Degrees Specified for Image Rotation is Not an Integer Multiple of 90 Degrees, then some Pixel Values Beyond Image Boundaries i.e Pixel values lying outside the Dimension of the image. Such Values will not be displayed in the output image.
Method:1 Using Image Processing Library Pillow
Python3
from
PIL
import
Image
Original_Image
=
Image.
open
[
"./gfgrotate.jpg"
]
rotated_image1
=
Original_Image.rotate[
180
]
rotated_image2
=
Original_Image.transpose[Image.ROTATE_90]
rotated_image3
=
Original_Image.rotate[
60
]
rotated_image1.show[]
rotated_image2.show[]
rotated_image3.show[]
Output:
This is Image is Rotated By 180 Degree
This Image Is Rotated By 60 Degree
This Image Is Rotated By 90 Degree
The rotate[] method of Python Image Processing Library Pillow Takes the number of degrees as a parameter and rotates the image in Counter Clockwise Direction to the number of degrees specified.
Method 2: Using Open-CV to rotate an image by an angle in Python
This is common that everyone knows that Python Open-CV is a module that will handle real-time applications related to computer vision. Open-CV works with image processing library imutilswhich deals with images. The imutils.rotate[] function is used to rotate an image by an angle in Python.
Python3
import
cv2
import
imutils
image
=
cv2.imread[r
".\gfgrotate.jpg"
]
Rotated_image
=
imutils.rotate[image, angle
=
45
]
Rotated1_image
=
imutils.rotate[image, angle
=
90
]
cv2.imshow[
"Rotated"
, Rotated_image]
cv2.imshow[
"Rotated"
, Rotated1_image]
cv2.waitKey[
0
]
Output:
Image Rotated Using Open-CV in 45 Degree
Image Rotated Using Open-CV in 90 Degree
Even this Open-CV Rotates the image in Counter Clockwise direction to the number of degrees specified