python opencv图片旋转_Python+OpenCV 实现图片无损旋转90°且无黑边的教程

#!/usr/bin/python

# -*- coding: UTF-8 -*-

"""

This program is debugged by Harden Qiu.

You can send a e-mail to hardenqiu@foxmail.com for more details.

"""

import numpy as np

import cv2

def main():

img = cv2.imread('.\\imgs\\img10.jpg')

height, width = img.shape[:2]

matRotate = cv2.getRotationMatrix2D((height * 0.5, width * 0.5), -90, 1)

dst = cv2.warpAffine(img, matRotate, (width, height*2))

rows, cols = dst.shape[:2]

for col in range(0, cols):

if dst[:, col].any():

left = col

break

for col in range(cols-1, 0, -1):

if dst[:, col].any():

right = col

break

for row in range(0, rows):

if dst[row,:].any():

up = row

break

for row in range(rows-1,0,-1):

if dst[row,:].any():

down = row

break

res_widths = abs(right - left)

res_heights = abs(down - up)

res = np.zeros([res_heights ,res_widths, 3], np.uint8)

for res_width in range(res_widths):

for res_height in range(res_heights):

res[res_height, res_width] = dst[up+res_height, left+res_width]

cv2.imshow('res',res)

cv2.imshow('img',img)

cv2.imshow('dst', dst)

cv2.waitKey(0)

if __name__ =='__main__':

main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值