Python 使用opencv将图像转换为视频

Python 使用opencv将图像转换为视频,python,image,opencv,image-recognition,cv2,Python,Image,Opencv,Image Recognition,Cv2,我有一个根文件夹/train\u images,其中有/train\u images/1、/train\u images/2……等子文件夹。子文件夹的Ech中包含图像。如果num(images)>4,我想制作每个子文件夹对应的视频。下面是我为同样的目的编写的代码。如果只有1个子文件夹,则它会工作,并在创建第二个Videowriter对象时停止。请帮忙 import cv2 import os root_folder = 'train_images' video_folder = 'output

我有一个根文件夹/train\u images,其中有/train\u images/1、/train\u images/2……等子文件夹。子文件夹的Ech中包含图像。如果num(images)>4,我想制作每个子文件夹对应的视频。下面是我为同样的目的编写的代码。如果只有1个子文件夹,则它会工作,并在创建第二个Videowriter对象时停止。请帮忙

import cv2
import os

root_folder = 'train_images'
video_folder = 'output_video'
folders = [folder for folder in os.listdir(root_folder)]
for folder in folders:
 child = os.path.join(root_folder,folder)
 images = [img for img in os.listdir(child) if img.endswith(".jpg")]
 print('%d images in the path %s' % (len(images),child))
 if(len(images)>=4):
    video_path ='{}.avi'.format(folder)
    print(video_path)
    print(os.path.join(video_folder, video_path))
    video = cv2.VideoWriter(os.path.join(video_folder, video_path), -1, 5, (224, 320))
    for img in images:
     frame = cv2.imread(os.path.join(child, img))
     print('writing from %s' %(os.path.join(child, img)) )
     frame_new=cv2.resize(frame,(224,320))
     #print(frame_new.shape)
     video.write(frame_new)

cv2.destroyAllWindows()
video.release()

请更详细地描述发生了什么。脚本是否停止执行(中断for循环)或是否存在错误?如果有错误,回溯是什么?我已经将视频编写器fourcc更改为“MJPG”,现在它可以工作了。。