旋转
import cv2
import numpy as np
def rotate_img(img, degrees):
'''
旋转图片
:param img: 原始图片
:param degrees: 旋转角度
:return:
'''
height, width = img.shape[:2]
# 旋转后的尺寸
heightNew = int(width * fabs(sin(radians(degrees))) + height * fabs(cos(radians(degrees))))
widthNew = int(height * fabs(sin(radians(degrees))) + width * fabs(cos(radians(degrees))))
# 这里的第一个参数为旋转中心,第二个为旋转角度,第三个为旋转后的缩放因子
# 可以通过设置旋转中心,缩放因子,以及窗口大小来防止旋转后超出边界的问题
matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degrees, 1)
matRotation[0, 2] += (widthNew - width) / 2
matRotation[1, 2] += (heightNew - height) / 2
resultImg = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))
return resultImg
if __name__ == '__main__':
img = cv2.imread('D:/test/