如何使用Python旋转图像?
在本文中,让我们看看如何使用Python旋转图像。通过图像旋转,图像围绕其中心旋转指定的度数。图像的旋转是一种几何变换。它可以通过正向变换(或)逆变换来完成。
这里带枕头的图像处理库使用逆变换。如果为图像旋转指定的度数不是 90 度的整数倍,则一些像素值超出图像边界,即位于图像维度之外的像素值。此类值不会显示在输出图像中。
方法:1使用图像处理库枕头
Python3
# import the Python Image
# processing Library
from PIL import Image
# Giving The Original image Directory
# Specified
Original_Image = Image.open("./gfgrotate.jpg")
# Rotate Image By 180 Degree
rotated_image1 = Original_Image.rotate(180)
# This is Alternative Syntax To Rotate
# The Image
rotated_image2 = Original_Image.transpose(Image.ROTATE_90)
# This Will Rotate Image By 60 Degree
rotated_image3 = Original_Image.rotate(60)
rotated_image1.show()
rotated_image2.show()
rotated_image3.show()
Python3
import cv2 # importing cv
import imutils
# read an image as input using OpenCV
image = cv2.imread(r".\gfgrotate.jpg")
Rotated_image = imutils.rotate(image, angle=45)
Rotated1_image = imutils.rotate(image, angle=90)
# display the image using OpenCV of
# angle 45
cv2.imshow("Rotated", Rotated_image)
# display the image using OpenCV of
# angle 90
cv2.imshow("Rotated", Rotated1_image)
# This is used for To Keep On Displaying
# The Image Untill Any Key is Pressed
cv2.waitKey(0)
输出:
Python图像处理库Pillow的rotate()方法以度数为参数,将图像逆时针旋转到指定的度数。
方法二:在Python中使用Open-CV将图像旋转一个角度
这一点大家都知道, Python Open-CV 是一个会处理与计算机视觉相关的实时应用的模块。 Open-CV 与图像处理库imutils 配合使用 处理图像。 imutils.rotate()函数用于在Python中将图像旋转一个角度。
蟒蛇3
import cv2 # importing cv
import imutils
# read an image as input using OpenCV
image = cv2.imread(r".\gfgrotate.jpg")
Rotated_image = imutils.rotate(image, angle=45)
Rotated1_image = imutils.rotate(image, angle=90)
# display the image using OpenCV of
# angle 45
cv2.imshow("Rotated", Rotated_image)
# display the image using OpenCV of
# angle 90
cv2.imshow("Rotated", Rotated1_image)
# This is used for To Keep On Displaying
# The Image Untill Any Key is Pressed
cv2.waitKey(0)
输出:
甚至这个 Open-CV 将图像以逆时针方向旋转到指定的度数