90度旋转图片无黑边

#旋转图片无黑边
def rotate_image():
    img =cv2.imread('2018-09-10IMG_8001.jpg')
    # img=cv2.imread('2018-09-10IMG_8003.jpg')
    height, width = img.shape[:2]
    if height < width:
        image = cv2.resize(img, (1600, 1200))
    else:
        degree = 90
        # 旋转后的尺寸
        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)

        print(matRotation[0, 2])
        matRotation[0, 2] += (widthNew - width) / 2  # 重点在这步,目前不懂为什么加这步
        matRotation[1, 2] += (heightNew - height) / 2  # 重点在这步


        imgRotation = cv2.warpAffine(img, matRotation, (widthNew, heightNew), borderValue=(255, 255, 255))

        image=cv2.resize(imgRotation,(1600,1200))
    cv2.imwrite('img_size_1.jpg',image)

原图:

旋转90度图

 这样旋转有边界

def rotate_image():
    img =cv2.imread('1.jpg')
    # img=cv2.imread('2018-09-10IMG_8003.jpg')
    height, width = img.shape[:2]
    if height < width:
        image = cv2.resize(img, (1600, 1200))
    else:
        degree = 90

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

        imgRotation = cv2.warpAffine(img, matRotation, (width, height))

        image=cv2.resize(imgRotation,(1600,1200))
    cv2.imwrite('img_size_1.jpg',image)

猜你喜欢

转载自blog.csdn.net/fanzonghao/article/details/82623722