File size: 1,174 Bytes
63f3cf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: UTF-8 -*-
'''=================================================
@Project -> File   localizer -> video_to_image
@IDE    PyCharm
@Author fx221@cam.ac.uk
@Date   13/01/2024 15:29
=================================================='''
import argparse
import os
import os.path as osp
import cv2

parser = argparse.ArgumentParser(description='Image2Video', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--image_path', type=str, required=True)
parser.add_argument('--video_path', type=str, required=True)
parser.add_argument('--height', type=int, default=-1)
parser.add_argument('--sample_ratio', type=int, default=-1)


def main(args):
    video = cv2.VideoCapture(args.video_path)
    nframe = 0
    while True:
        ret, frame = video.read()
        if ret:
            if args.sample_ratio > 0:
                if nframe % args.sample_ratio != 0:
                    nframe += 1
                    continue
            cv2.imwrite(osp.join(args.image_path, '{:06d}.png'.format(nframe)), frame)
            nframe += 1
        else:
            break


if __name__ == '__main__':
    args = parser.parse_args()
    main(args=args)