Opencv python 图片生成视频的方法示例
作者:廷益--飞鸟 时间:2021-11-08 22:18:38
本文主要介绍了Opencv图片生成视频,分享给大家,具体如下:
生成视频
import random as rd
import cv2 as cv
import numpy as np
# 保存视频
class RecordMovie(object):
def __init__(self, img_width, img_height):
self.video_writer = None # 视频对象
self.is_end = False # 结束保存视频
self.img_width = img_width # 宽度
self.img_height = img_height # 高度
# 创建 视频写入对象
def start(self, file_name, freq):
# 创建视频格式
four_cc = cv.VideoWriter_fourcc(*'mp4v')
img_size = (self.img_width, self.img_height) # 视频尺寸
# 创建视频写入对象
self.video_writer = cv.VideoWriter()
self.video_writer.open(file_name, four_cc, freq, img_size, True)
# 写入图片帧
def record(self, img):
if self.is_end is False:
self.video_writer.write(img)
# 完成视频 释放资源
def end(self):
self.is_end = True
self.video_writer.release()
def move_image(img_src):
img_height, img_width = img_src.shape[:2]
# 随机 xy平移方向与大小设置
x_size = rd.randint(-3, 3)
y_size = rd.randint(-3, 3)
# 自定义转换矩阵
transform_matrix = np.float32([[1, 0, x_size], [0, 1, y_size]])
# 执行平移
return cv.warpAffine(img_src, transform_matrix, (img_width, img_height))
def main():
# 1.读取图片
img_org = cv.imread("img.png", cv.IMREAD_GRAYSCALE)
# 2.显示图片
cv.imshow("org", img_org)
cv.namedWindow("shift")
# 3.视频文件生成
height, width = img_org.shape[:2]
print(height, width)
rm = RecordMovie(width, height)
# 设置视频文件名称 频率
rm.start("test.mp4", 20)
# 4.图片写入视频
for i in range(300):
# 图片微调调整
img_move = move_image(img_org)
img_move = cv.cvtColor(img_move, cv.COLOR_GRAY2RGB)
rm.record(img_move)
cv.imshow("shift", img_move)
key = cv.waitKey(10)
if key == 27: # esc 按键
break
# 5.关闭视频文件
rm.end()
if __name__ == '__main__':
main()
来源:https://blog.csdn.net/weixin_45875105/article/details/109741575
标签:Opencv,图片,视频
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python构建自定义回调函数详解
2023-09-07 03:30:45
python文件路径操作方法总结
2023-04-30 21:00:15
用FrontPage制作缩略图和图片重叠效果
2007-11-18 14:45:00
Python中下划线的使用方法
2021-10-05 23:27:12
使用PyV8在Python爬虫中执行js代码
2022-05-09 14:33:36
Python实现抓取城市的PM2.5浓度和排名
2023-07-01 11:13:33
![](https://img.aspxhome.com/file/2023/4/91984_0s.jpg)
python把数组中的数字每行打印3个并保存在文档中的方法
2022-08-13 19:15:30
![](https://img.aspxhome.com/file/2023/8/78658_0s.jpg)
基于python实现音乐播放器代码实例
2022-07-17 21:55:53
纯CSS无限级下拉菜单
2009-09-17 11:29:00
PHP实现的AES加密、解密封装类与用法示例
2023-07-23 12:56:45
Python编程实现生成特定范围内不重复多个随机数的2种方法
2022-05-08 08:49:51
![](https://img.aspxhome.com/file/2023/3/95333_0s.jpg)
PHP下常用正则表达式整理
2023-11-18 03:04:48
python实现定时提取实时日志程序
2023-03-03 22:31:24
Python 遍历子文件和所有子文件夹的代码实例
2021-02-09 17:21:34
![](https://img.aspxhome.com/file/2023/9/78589_0s.png)
Python数据类型之String字符串实例详解
2022-01-08 11:38:50
Python中if语句的基本格式实例代码
2023-12-02 14:31:20
Mootools 1.2教程(22)——同时进行多个形变动画
2008-12-29 14:11:00
XML:OpenSearch 浏览器指定搜索应用
2010-05-04 19:37:00
![](https://img.aspxhome.com/file/UploadPic/20105/4/2008101720221064077801-78s.png)
Python实现的批量修改文件后缀名操作示例
2021-08-28 08:34:58
![](https://img.aspxhome.com/file/2023/3/101643_0s.png)
SQL Server 总结复习(一)
2012-10-07 11:04:02