cv.imread()读取图片
cv.imread('name',img) 显示图片
cv.waitKey() 窗口等待函数
import cv2
import numpy as np
import random
from PIL import Image
def random_rotate(image,
angles: list,
center = None,
scale_factor= 1.0) -> np.ndarray:
"""Rotate an image by a random angle
Args:
image (np.ndarray): Image read with OpenCV
angles (list): Rotation angle range
center (optional, tuple[int, int]): High resolution image selection center point. Default: ``None``
scale_factor (optional, float): scaling factor. Default: 1.0
Returns:
rotated_image (np.ndarray): image after rotation
"""
image_height, image_width = image.shape[:2]
if center is None:
center = (image_width // 2, image_height // 2)
# Random select specific angle
angle = random.choice(angles)
matrix = cv2.getRotationMatrix2D(center, angle, scale_factor)
rotated_image = cv2.warpAffine(image, matrix, (image_width, image_height))
return rotated_image
if __name__=='__main__':
# img=Image.open('./1.png')
# img.show()
# img=random_rotate(np.array(img),[90,45])
# img=Image.fromarray(img)
# img.show()
img=cv2.imread('./1.png')
img=random_rotate(img,angles=[45,45])
# cv2.imshow('hh',img)
# cv2.waitKey(10000)
img=Image.fromarray(img)
img.show()
注意 opencv读取的图片是array类型,图片格式是bgr。PIL读取的图片是 pil类型,图片的格式是rgb。所以两者在使用要注意相互转换,否则容易出现错误。