对原始图像进行旋转,生成新的图像数据,并存储操作的代码
时间: 2024-02-13 07:02:34 浏览: 25
下面是对原始图像进行旋转,生成新的图像数据,并存储操作的Python代码:
``` python
import os
import cv2
# 定义旋转角度列表
degrees = [90, 180, 270]
# 定义原始图像路径和目标文件夹路径
src_path = 'path/to/source/images/'
dst_path = 'path/to/output/folder/'
# 遍历原始图像
for root, dirs, files in os.walk(src_path):
for file in files:
# 读取原始图像
img = cv2.imread(os.path.join(root, file))
# 遍历旋转角度列表
for degree in degrees:
# 获取旋转矩阵
M = cv2.getRotationMatrix2D((img.shape[1] / 2, img.shape[0] / 2), degree, 1)
# 进行旋转
rotated_img = cv2.warpAffine(img, M, (img.shape[1], img.shape[0]))
# 保存旋转后的图像
new_file_name = os.path.splitext(file)[0] + '_rotated_' + str(degree) + os.path.splitext(file)[1]
cv2.imwrite(os.path.join(dst_path, new_file_name), rotated_img)
```
其中,`degrees`变量定义了旋转角度列表,`src_path`变量定义了原始图像路径,`dst_path`变量定义了目标文件夹路径。代码通过遍历原始图像,依次进行旋转,并将旋转后的图像保存到目标文件夹中。需要注意的是,代码中使用了OpenCV库进行图像处理,需要先安装该库。