python-opencv对图像进行旋转

#coding=utf8
import os,cv2,shutil
import numpy as np
import random
 
#旋转任意度数,旋转之后图像的宽高并没有交换
def rotate(image, angle, scale=1):
    w = image.shape[1]
    h = image.shape[0]
    #rotate matrix
    M = cv2.getRotationMatrix2D((w/2,h/2), angle, scale)
    #rotate
    image = cv2.warpAffine(image,M,(w,h))
    return image

#顺时针旋转90度,旋转之后图像宽高被交换
def Rotate90(img):
    trans_img = cv2.transpose(img)
    new_img = cv2.flip(trans_img, 1)
    return new_img

#逆时针旋转90度,旋转之后图像宽高被交换
def Rotate_90(img):
    trans_img = cv2.transpose( img )
    new_img = cv2.flip( trans_img, 0 )
    return new_img


if __name__ == "__main__":
    path='./ocr_crop'
    save_path = './ocr_crop_rotate'
    for image_name in os.listdir(path):
        image_path = os.path.join(path, image_name)
        print(image_path)
        img = cv2.imread(image_path)
        print("img.shape[1]:",img.shape[1])
        print("img.shape[0]:",img.shape[0]) 
        img_rotate = Rotate90(img)
        print("img_rotate.shape[1]:",img_rotate.shape[1])
        print("img_rotate.shape[0]:",img_rotate.shape[0]) 
        cv2.imwrite(os.path.join(save_path, image_name), img_rotate)

猜你喜欢

转载自blog.csdn.net/u013171226/article/details/115163514