python從圖片到視頻

綜述

python 從圖片變爲視頻

代碼

import cv2
import numpy as np
import os
from os.path import isfile, join
pathIn= './im3/'  # img sequence 注意: 您的index應該爲諸如 : 1.png 2.png 之類
pathOut = 'videoscans_10000_smooth_7.avi'
fps = 7
frame_array = []
files = [f for f in os.listdir(pathIn) if isfile(join(pathIn, f))]
#for sorting the file names properly
files.sort(key= lambda x:int(x[:-4]))
frame_array = []

for i in range(len(files)):
    filename = pathIn + files[i]
    # reading each files
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width, height)
    # inserting the frames into an image array
    frame_array.append(img)

out = cv2.VideoWriter(pathOut, cv2.VideoWriter_fourcc(*'DIVX'), fps, size)
for i in range(len(frame_array)):
    # writing to a image array
    out.write(frame_array[i])
out.release()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章