python opencv旋转图像(保持图像不被裁减)

yipeiwu_com6年前Python基础

本文实例为大家分享了python opencv旋转图像的具体代码,保持图像不被裁减,供大家参考,具体内容如下

# -*- coding:gb2312 -*-
import cv2
from math import *
import numpy as np

img = cv2.imread("3-2.jpg")

height,width=img.shape[:2]

degree=45
#旋转后的尺寸
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)

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中关于时间和日期函数的常用计算总结(time和datatime)

1.获取当前时间的两种方法: 复制代码 代码如下:import datetime,timenow = time.strftime("%Y-%m-%d %H:%M:%S")print no...

python 从csv读数据到mysql的实例

如下所示: import csv import sys,os import MySQLdb def read_csv(filename): with open(filename...

Python 操作 ElasticSearch的完整代码

Python 操作 ElasticSearch的完整代码

官方文档:https://elasticsearch-py.readthedocs.io/en/master/   1、介绍     python提供了操作ElasticSearch 接...

Python基于回溯法子集树模板解决野人与传教士问题示例

Python基于回溯法子集树模板解决野人与传教士问题示例

本文实例讲述了Python基于回溯法子集树模板解决野人与传教士问题。分享给大家供大家参考,具体如下: 问题 在河的左岸有N个传教士、N个野人和一条船,传教士们想用这条船把所有人都运过河去...

python如何压缩新文件到已有ZIP文件

本文为大家分享了python压缩新文件到已有ZIP文件的具体代码,供大家参考,具体内容如下 要点在于使用Python标准库zipfile创建压缩文件时,如果使用'a'模式时,可以追加新内...