开发板:潘多拉开发板
系统版本:v4.0.3
一、将视频转换成图片
gif 和视频转换的方法都是一样的,这是我选择使用的是太空人的 gif。
转换的方法就是通过 python + opencv 从视频或 gif 中按帧提取图片,然后保存图片。
参考代码如下。
import os
import cv2
class Picture(object):
def __init__(self, video_path, pictures_path):
self.video_path = video_path
self.pictures_path = os.path.normpath( os.getcwd() + pictures_path )
def find_video(self):
if os.path.isfile(self.video_path) == False:
print("voide %s dont find." % self.video_path)
return False
self.picture_name = self.video_path.split('/')[-1] # 设置图片的名称
self.picture_name = self.picture_name.split('.')[0]
def open_video(self):
self.vc = cv2.VideoCapture(self.video_path) # 读入视频文件
self.cnt = 0 # 统计对应帧号
ret = self.vc.isOpened() # 判断视频是否打开
if ret == False:
print("voide %s open failed." % self.video_path)
return ret
def get_video_info(self):
self.width = self.vc.get(3) # 视频宽度
self.heigth = self.vc.get(4) # 视频高度
self.frame_num = self.vc.get(7) # 获取视频总帧数
print("width:%d height:%d frame:%d." % (self.width, self.heigth, self.frame_num))
def close_video(self):
self.vc.release()
def set_picture_param(self, width, heigth):
if (width != 0 and heigth != 0):
self.frame = cv2.resize(self.frame,