Python实现图片与视频互转代码实战(亲测有效)
作者:X_Imagine 时间:2021-05-30 17:21:03
一、 图片转视频
任务需求背景
在标注数据的过程中,需要【反复】浏览大量图片(万张以上的数量级),确认图片中的目标类别以及室内户型布局。但是,在电脑上浏览图片有很大的不足:(a)需要持续点击鼠标或者键盘;(b)图片加载跟不上点击速度。
值得注意的是:网上有很多代码(图片转视频),但是真正能用的几乎很少,本博文的代码经过测试,可以成功生成视频。
代码依赖库
opencv-python==4.5.2
numpy==1.19.2
glob(python自带模块)
代码实战
基本步骤如下:
a. 使用glob
获取路径下的所有图片;
b.cv2.imread()
读取所有图片;
c. 将读取的图片存于列表中;
d. 使用cv2.VideoWriter()
创建VideoWriter
对象,注意参数的设置;
e. 使用cv2.VideoWriter().write()
保存每一帧图像到视频文件;
f. 释放VideoWriter
对象;
import cv2
import numpy as np
import glob
import os
# 其它格式的图片也可以
img_array = []
for filename in glob.glob('E:/3DS1Data/20211118/29984CRL30V00067087/dataset/rgb/*.png'):
img = cv2.imread(filename)
height, width, layers = img.shape
size = (width, height)
img_array.append(img)
# avi:视频类型,mp4也可以
# cv2.VideoWriter_fourcc(*'DIVX'):编码格式
# 5:视频帧率
# size:视频中图片大小
out = cv2.VideoWriter('E:/3DS1Data/20211118/29984CRL30V00067087/dataset/project-all.avi',
cv2.VideoWriter_fourcc(*'DIVX'),
5, size)
for i in range(len(img_array)):
out.write(img_array[i])
out.release()
下图是原始图片数据:
生成的视频文件(播放中):
来源:https://blog.csdn.net/kxh123456/article/details/121692474
标签:Python,图片,视频,互转
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 3.7.0 安装配置方法图文教程
2023-10-14 22:24:28
![](https://img.aspxhome.com/file/2023/2/65522_0s.jpg)
python实现登录密码重置简易操作代码
2022-12-08 13:34:43
在ASP中使用SQL语句之1:SELECT 语句
2007-08-11 12:18:00
Python中的Function定义方法第1/2页
2021-05-10 20:33:49
python 实时遍历日志文件
2021-03-30 09:26:20
对于Python中线程问题的简单讲解
2023-12-28 10:16:31
numpy中轴处理的实现
2021-08-14 12:06:16
![](https://img.aspxhome.com/file/2023/0/95520_0s.png)
python 发送邮件的四种方法汇总
2022-04-09 05:44:18
pytorch 计算ConvTranspose1d输出特征大小方式
2021-08-14 10:16:16
![](https://img.aspxhome.com/file/2023/3/118083_0s.jpg)
[翻译]标记语言和样式手册 chapter 6 短语元素
2008-01-25 16:37:00
![](https://img.aspxhome.com/file/UploadPic/20081/25/200812517182403s.jpg)
python基础之引用和匿名函数
2023-01-13 11:52:42
![](https://img.aspxhome.com/file/2023/3/81563_0s.jpg)
virtualenv实现多个版本Python共存
2021-11-27 00:50:44
![](https://img.aspxhome.com/file/2023/6/64706_0s.png)
Tensorflow2.4从头训练Word Embedding实现文本分类
2023-05-22 20:03:17
详解css定位与定位应用
2007-05-11 16:52:00
![](https://img.aspxhome.com/file/uploadpic/20075/2007511165230659.png)
详解Python绘图Turtle库
2021-11-29 05:42:06
![](https://img.aspxhome.com/file/2023/3/65903_0s.png)
Python列表去重复项的N种方法(实例代码)
2023-06-27 16:00:20
![](https://img.aspxhome.com/file/2023/0/60710_0s.jpg)
Python实现K折交叉验证法的方法步骤
2021-08-04 13:58:25
python实现csv格式文件转为asc格式文件的方法
2021-10-12 19:10:37
![](https://img.aspxhome.com/file/2023/3/72823_0s.jpg)
Python闭包和装饰器用法实例详解
2021-04-07 10:05:02
python正则实现提取电话功能
2022-04-30 03:32:46