forked from WoodsGao/cv_utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
img2video.py
executable file
·38 lines (33 loc) · 1.19 KB
/
img2video.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
#!/usr/bin/python3
import argparse
import os
import os.path as osp
import cv2
from tqdm import tqdm
def img2video(img_dir, fps, img_size):
names = os.listdir(img_dir)
names = [name for name in names if osp.splitext(name)[1] in ['.png', '.tiff', '.jpg', '.jpeg']]
names.sort()
video_writer = cv2.VideoWriter(img_dir + '.avi',
cv2.VideoWriter_fourcc(*'H264'), fps, img_size)
for name in tqdm(names):
img = cv2.imread(osp.join(img_dir, name))
if img is None:
continue
if img.shape[0] != img_size[1] or img.shape[1] != img_size[0]:
img = cv2.resize(img, img_size)
video_writer.write(img)
video_writer.release()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--img-dir', type=str)
parser.add_argument('--fps', type=int, default=60)
parser.add_argument('--img-size', type=str)
opt = parser.parse_args()
img_size = opt.img_size.split(',')
assert len(img_size) in [1, 2]
if len(img_size) == 1:
img_size = [int(img_size[0])] * 2
else:
img_size = [int(x) for x in img_size]
img2video(opt.img_dir, opt.fps, img_size)