import cv2 img = cv2.imread("./origin.jpg") h, w = img.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, 45, 0.5) rotated = cv2.warpAffine(img, M, (w, h)) cv2.imwrite("./rotated.jpg", rotated)
原图像:
rotated_45.jpg:
rotated_-90.jpg:
实现代码:
# encoding:utf-8 import cv2 img = cv2.imread("./girl.jpg") h, w = img.shape[:2] center = (w // 2, h // 2) # 旋转中心坐标,逆时针旋转:45°,缩放因子:0.5 M_1 = cv2.getRotationMatrix2D(center, 45, 0.5) rotated_1 = cv2.warpAffine(img, M_1, (w, h)) cv2.imwrite("./rotated_45.jpg", rotated_1) # 旋转中心坐标,逆时针旋转:-90°,缩放因子:1 M_2 = cv2.getRotationMatrix2D(center, -90, 1) rotated_2 = cv2.warpAffine(img, M_2, (w, h)) cv2.imwrite("./rotated_-90.jpg", rotated_2)
但是不管怎么旋转,该图像的shape始终和原图像一致。这在某种程度上会造成不好的影响。
如果想要无损地旋转图像,请参看我的另一篇blog:图像处理: 无损地旋转图像。
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句