python实现图像的翻转、倒置、旋转和平移的三种方法代码+注释 您所在的位置:网站首页 图像旋转有3个关键要素分别是 python实现图像的翻转、倒置、旋转和平移的三种方法代码+注释

python实现图像的翻转、倒置、旋转和平移的三种方法代码+注释

2023-07-17 08:40| 来源: 网络整理| 查看: 265

        草书检测搞不出,整点小知识学一下:

# -*- coding:utf-8 -*- # coding=utf-8 #coco #三种方法实现对图片实现旋转、倒置、翻转 import os import uuid import cv2 as cv from PIL import Image from ffmpy import FFmpeg import matplotlib.pyplot as plt import numpy as np '''---------------------------------------------------方法一-------------------------------------------------------''' # 垂直翻转 def vflip(image_path: str, output_dir: str): ext = _check_format(image_path) result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext)) ff = FFmpeg(inputs={image_path: None}, outputs={result: '-vf vflip -y'}) print(ff.cmd) ff.run() return result # 水平翻转 def hflip(image_path: str, output_dir: str): ext = _check_format(image_path) result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext)) ff = FFmpeg(inputs={image_path: None}, outputs={result: '-vf hflip -y'}) print(ff.cmd) ff.run() return result # 顺时针旋转 def rotate(image_path: str, output_dir: str, angle: int): ext = _check_format(image_path) result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext)) ff = FFmpeg(inputs={image_path: None}, outputs={result: '-vf rotate=PI*{}/180 -y'.format(angle)}) print(ff.cmd) ff.run() return result # 转置 ''' type:0 逆时针旋转90度,对称翻转 type:1 顺时针旋转90度 type:2 逆时针旋转90度 type:3 顺时针旋转90度,对称翻转 ''' #逆时针旋转 def transpose(image_path: str, output_dir: str, type: int): ext = _check_format(image_path) result = os.path.join(output_dir, '{}.{}'.format(uuid.uuid4(), ext)) ff = FFmpeg(inputs={image_path: None}, outputs={result: '-vf transpose={} -y'.format(type)}) print(ff.cmd) ff.run() return result #获得图片的后缀 def _check_format(image_path: str): ext = os.path.basename(image_path).strip().split('.')[-1] if ext not in ['png', 'jpg']: raise Exception('format error') return ext '''-------------------------------------------------方法二-----------------------------------------------------''' def method_two(): path='E:/project/Template_detection/Image_preprocessing/cake.jpg' image = Image.open(path) image.show() #左右水平翻转 #out =image.transpose(Image.FLIP_LEFT_RIGHT) #上下翻转 #out = image.transpose(Image.FLIP_TOP_BOTTOM) #顺时针旋转90度 #out = image.transpose(Image.ROTATE_90) #逆时针旋转45度 out = image.rotate(45) out.save('E:/project/Template_detection/Image_preprocessing/cake_5.png','png') '''--------------------------------------------------方法三--------------------------------------------------------''' def method_three(): path = 'E:/project/Template_detection/Image_preprocessing/cake.jpg' image = cv.imread(path) '''改变图像的大小''' image1 = cv.resize(image,(400,400)) '''图像旋转 rows, cols, chnl = image.shape # 旋转参数:旋转中心,旋转角度,scale #旋转角度'-'顺时针旋转,'+'逆时针旋转 M = cv.getRotationMatrix2D((cols / 2, rows / 2), -60, 1) # 参数:原始图像,旋转参数,元素图像宽高 rotated = cv.warpAffine(image, M, (cols, rows)) # 图片显示 cv.imshow("rotated", rotated) cv.imshow("image", image) # 等待窗口 cv.waitKey(0) cv.destroyAllWindows()''' '''图像翻转 # 灰度处理 scr = cv.cvtColor(image, cv.COLOR_BGR2RGB) # 图像翻转 # 0以X轴对称翻转,>0以Y轴对称翻转,


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有