python-opencv旋转图像,保持图像不被裁减

python-opencv旋转图像,保持图像不被裁减

import cv2
from math import *

def remote(img,degree):
    #degree左转
    img = cv2.imread(img)
    height, width = img.shape[:2]
    heightNew = int(width * fabs(sin(radians(degree))) + height * fabs(cos(radians(degree))))
    widthNew = int(height * fabs(sin(radians(degree))) + width * fabs(cos(radians(degree))))

    matRotation = cv2.getRotationMatrix2D((width / 2, height / 2), degree, 1)

    matRotation[0, 2] += (widthNew - width) / 2
    matRotation[1, 2] += (heightNew - height) / 2
    imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))

    cv2.imshow("img", img)
    cv2.imshow("imgRotation", imgRotation)
    cv2.waitKey(0)
remote('1.jpg',90)
原文地址:https://www.cnblogs.com/timssd/p/10235960.html