python OpenCV numpy旋转图片

本文介绍了如何使用Python的PIL库实现图片的顺时针旋转和翻转,包括90度、180度以及水平和垂直方向的翻转。通过实例展示了如何对图片进行这些操作,并提供了相应的代码片段。
def Rollover_Image(img_file):  # np.rot90(img, 1) 顺时针旋转90度
    img = cv2.imread(img_file,cv2.IMREAD_UNCHANGED)
    # np.rot90(img, 1) 顺时针旋转90度
    # np.rot90(img, 2) #顺时针旋转180度
    # np.rot90(img, 3) 顺时针旋转270度
    # np.flipud(img) 竖直翻转图片

    img180=np.flipud(img)
    # cv2.imwrite(test/1_test2.jpg",img180)
    # cv2.imshow("rotate", img180)
    # cv2.imshow("img", img)
    # cv2.waitKey(0)
    return img180

# 翻转图片
from PIL import Image

def flip_image(img_path,output_flip_left_right,output_flip_top_bottom):
    img = Image.open(img_path)
    img = img.convert("RGB") # 解决:png格式图片颜色为四通道RGBA,而jpg格式是三通道RGB,保存报错

    flip_left_right = img.transpose(Image.FLIP_LEFT_RIGHT)  # 水平翻转
    flip_top_bottom = img.transpose(Image.FLIP_TOP_BOTTOM)  # 垂直翻转
    flip_left_right.save(output_flip_left_right)
    flip_top_bottom.save(output_flip_top_bottom)

if __name__ == '__main__':
    img_path= 'D:/daily/04/0411/1.jpg'
    output_flip_left_right = 'D:/daily/04/0411/flip_left_right.jpg'
    output_flip_top_bottom = 'D:/daily/04/0411/flip_top_bottom.jpg'
    flip_image(img_path,output_flip_left_right,output_flip_top_bottom)

在使用 PythonOpenCV 批量随机旋转图像时,可以通过以下步骤实现。首先,读取目录下的所有图像文件,然后对每张图像应用随机旋转角度,并保存处理后的图像到指定路径。 ### 实现方法 1. **导入必要的库** 需要导入 `cv2`(OpenCVPython 接口)、`numpy` 以及 `os` 模块来处理图像和文件系统操作。 2. **定义旋转函数** 使用 `cv2.warpAffine` 函数进行仿射变换,通过 `cv2.getRotationMatrix2D` 获取旋转矩阵,从而实现图像的任意角度旋转。 3. **批量处理图像** 遍历指定目录中的图像文件,为每张图像生成一个随机旋转角度(例如从 [-90, -45, 45, 90] 中选择),并应用旋转操作。 以下是完整的代码示例: ```python import cv2 import numpy as np import os import random def rotate_image(image, angle): # 获取图像中心坐标 center = (image.shape[1] // 2, image.shape[0] // 2) # 获取旋转矩阵 rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0) # 计算旋转后图像的边界尺寸 abs_cos = abs(rotation_matrix[0, 0]) abs_sin = abs(rotation_matrix[0, 1]) bound_w = int(image.shape[1] * abs_cos + image.shape[0] * abs_sin) bound_h = int(image.shape[1] * abs_sin + image.shape[0] * abs_cos) # 调整旋转矩阵以考虑平移部分 rotation_matrix[0, 2] += bound_w / 2 - center[0] rotation_matrix[1, 2] += bound_h / 2 - center[1] # 应用仿射变换 rotated_image = cv2.warpAffine(image, rotation_matrix, (bound_w, bound_h)) return rotated_image # 设置输入输出路径 input_folder = 'path/to/your/images' output_folder = 'path/to/save/rotated/images' # 创建输出目录(如果不存在) os.makedirs(output_folder, exist_ok=True) # 支持的图像扩展名 image_extensions = ['.jpg', '.jpeg', '.png', '.bmp'] # 遍历输入目录中的文件 for filename in os.listdir(input_folder): file_path = os.path.join(input_folder, filename) if any(filename.lower().endswith(ext) for ext in image_extensions): img = cv2.imread(file_path) # 生成随机旋转角度 angle = random.choice([-90, -45, 45, 90]) # 旋转图像 rotated_img = rotate_image(img, angle) # 构造输出文件路径 output_path = os.path.join(output_folder, f"rotated_{angle}_{filename}") # 保存旋转后的图像 cv2.imwrite(output_path, rotated_img) print("图像旋转完成并已保存至指定目录") ``` ### 注意事项 - **插值方法**:在图像旋转过程中,默认使用的插值方法是 `cv.INTER_LINEAR`,适用于大多数情况[^1]。 - **边界填充**:为了防止图像旋转后被裁剪,代码中计算了新的图像尺寸并调整了旋转矩阵以确保整个图像内容完整保留。 - **性能优化**:如果需要处理大量图像,可以结合多线程或异步处理来提升效率。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值