forked from aimuch/AITools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage2video.py
69 lines (50 loc) · 1.88 KB
/
image2video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
import os
import sys
import glob
from tqdm import tqdm
import argparse
import numpy as np
import cv2
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('image_dir', help='image directory', type=str)
parser.add_argument('output_dir', help='output vedeo directory ', type=str)
parser.add_argument('--fps', help='frames per second', default=10, type=int)
args = parser.parse_args()
return args
def img2video(image_dir, output_dir, fps):
img_list = os.listdir(image_dir)
# height, width, channels
img_shape = cv2.imread(os.path.join(image_dir, img_list[0])).shape[:2]
print(img_shape)
# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'XVID') # .avi
# fourcc = cv2.VideoWriter_fourcc(*'MJPG')
fourcc = cv2.VideoWriter_fourcc('M','J','P','G')
## cv2.Size=(width, height)
videoWriter = cv2.VideoWriter(os.path.join(output_dir, 'output.avi'), fourcc, fps, (img_shape[1], img_shape[0]))
for i in tqdm(img_list):
filename = os.path.join(image_dir, i)
# print(filename)
img = cv2.imread(filename)
if img is None:
continue
# print(img.shape)
videoWriter.write(img)
# Release everything if job is finished
videoWriter.release()
if __name__ == '__main__':
args = parse_args()
image_dir = args.image_dir
image_dir = os.path.abspath(image_dir)
output_dir = args.output_dir
output_dir = os.path.abspath(output_dir)
fps = args.fps
if not os.path.exists(image_dir):
print("Error !!! %s is not exists, please check the parameter"%image_dir)
sys.exit(0)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
img2video(image_dir, output_dir, fps)
print("Done !")