读取文件夹内所有视频,然后每秒截一帧图像 python opencv

本文介绍了一种使用Python和OpenCV将视频文件分解为单个图像帧的方法。通过遍历指定目录下的所有视频文件,该脚本可以按每24帧抽取一帧的方式将视频转换为图像,有效地进行视频数据的预处理。
#coding=utf-8
import cv2
import numpy as np
#获得文件夹内所有视频的名字
import  os

'''
for each vedio in vedios
    分解视频成图像
'''
SourceImgPath = "C:\\PycharmProjects\\VediosToImgs\\Vedio" + '\\'  # 视频读取路径
vedionamelist = os.listdir(SourceImgPath)  # 获得所有视频名字列表

ImgWritePath = "C:\\PycharmProjects\\VediosToImgs\\Img" + '\\'  # 图像保存路径
img_end = ".jpg"
img_start = 0


for vedio_path in vedionamelist:
    VedioPath = os.path.join('%s%s' % (SourceImgPath, vedio_path))  # 获得文件夹下所有文件的路径   读取路径和保存路径
    cap = cv2.VideoCapture(VedioPath)
    while cap.read():
        # get a frame
        ret, frame = cap.read()
        if ret == False:
            break #读到文件末尾

        # 显示第几帧
        frames_num = cap.get(cv2.CAP_PROP_POS_FRAMES)
        # 显示实时帧数
        FPS = cap.get(cv2.CAP_PROP_FPS)
        # 总帧数
        total_frame = cap.get(cv2.CAP_PROP_FRAME_COUNT)
        # show information of frame
        # cv2.putText(frame, "FPS:"+str(FPS), (17, 13), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255))
        # cv2.putText(frame, "NUM OF FRAME:"+str(frames_num), (222, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255))
        # cv2.putText(frame, "TOTAL FRAME:" + str(total_frame), (504, 17), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 255))
        # show a frame
        cv2.imshow("capture", frame)
        # img name
        img_name = str(img_start) + img_end
        img_start = img_start + 1
        # 存储
        if frames_num % 24 == 0:
            cv2.imwrite(ImgWritePath + img_name, frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()



好的,在Python中使用OpenCV图像合成为视频是一个常见的任务,下面我来详细解释一下步骤: ### 使用OpenCV图像序列转换成视频 #### 1. 安装必要的库 首先需要确保安装了`opencv-python`库。可以使用pip进行安装: ```bash pip install opencv-python ``` #### 2. 准备好图像文件 假设所有的图片都存放在一个文件夹下,并按照顺序命名(例如img_001.jpg、img_002.jpg等)。如果名字不是连续的数字,也可以通过其他方式进行排序。 #### 3. 编写脚本读取并处理图像 下面是具体的代码示例: ```python import cv2 import os from natsort import natsorted # 设置保存路径和名称 output_video = 'output.mp4' image_folder = './images/' # 放置所有用于合成视频的静态图所在的目录 # 获取该文件夹下的所有jpg/png格式的文件名列表,并自然排序 images = [img for img in os.listdir(image_folder) if img.endswith(".png") or img.endswith('.jpg')] sorted_images = natsorted(images) # 检查是否有可用的图片 if not sorted_images: print("找不到可用于生成视频的图片") else: frame = cv2.imread(os.path.join(image_folder, sorted_images[0])) height, width, layers = frame.shape fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 视频编码器选择MP4V,适用于大多数平台播放 video_writer = cv2.VideoWriter(output_video, fourcc, 25, (width,height)) # 创建VideoWrite对象,每秒25 try: for image_name in sorted_images: path_to_image_file = os.path.join(image_folder,image_name) current_frame = cv2.imread(path_to_image_file) resized_current_frame=cv2.resize(current_frame,(width,height),interpolation = cv2.INTER_AREA)#保证每一幅画尺寸一致 video_writer.write(resized_current_frame) print(f"成功创建{output_video}") finally: video_writer.release() # 关闭写作流 print('完成') ``` 此段程序实现了从指定文件夹加载一系列图像并将它们合并到单个视频输出的功能。它会遍历给定文件夹内的所有JPEG或PNG格式的照片,然后依次添加至新建立的AVI或MP4格式容器之中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值