1. 程式人生 > 其它 >Python OpenCV圖片轉視訊 工具貼(三)

Python OpenCV圖片轉視訊 工具貼(三)

Python OpenCV圖片轉視訊

貼上即用,注意使用時最好把自己的檔案按照數字順序命名。按照引導輸入操作。

# 一鍵傻瓜式引導圖片串成視訊
# 注意使用前最好把檔案命名為數字順序格式
import os
import cv2

def frame2video(image_path, save_path, fps, size):
    fourcc = cv2.VideoWriter_fourcc(*'I420')
    video_writer = cv2.VideoWriter(save_path, fourcc, fps, size)

    file_list = os.listdir(image_path)
    # print(file_list)
    counter = 1
    for file in file_list:
        print(counter)
        counter += 1
        image = image_path + file
        image = cv2.imread(image)
        video_writer.write(image)
    video_writer.release()
	print("we are done!")
    
def guide():
    image_path = input("請輸入圖片儲存路徑,注意在最後要加\或/:")
    save_path = image_path + "video.avi"
    fps = int(input("請輸入每秒鐘的幀數:"))
    width = input("請輸入圖片的寬:")
    height = input("請輸入圖片的高:")
    size = (int(width), int(height))
    return image_path, save_path, fps, size

if __name__ == "__main__":
    image_path, save_path, fps, size = guide()
    frame2video(image_path, save_path, fps, size)