python製作視頻
import os
import cv2
from PIL import Image
def makevideo(path, fps):
""" 將圖片合成視頻. path: 視頻路徑,fps: 幀率 """
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
images = os.listdir('./tu4')
im = Image.open('./tu4/' + images[0])
print(im.size)
vw = cv2.VideoWriter(path, fourcc, fps, im.size)
for image in tqdm(range(0, data.shape[0])):
imgfile = str(image + 1) + '.png'
try:
frame = cv2.imread('./tu4/%d.png'%image)
vw.write(frame)
except Exception as exc:
print(imgfile, exc)
vw.release()
if __name__ == '__main__':
video_path = 'test_new_ppt_16fps.avi'
makevideo(video_path, 16)