I couldn't find it when I looked for it, so I made it. Resize the video file that can be opened with OpenCV while thinning it out at appropriate frames and save it as an image file.
I haven't done anything difficult, so I don't think it's necessary to explain anything.
video2jpg.py
import cv2
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--video_path", type=str)
parser.add_argument("--save_dir", type=str, default="save/")
parser.add_argument("--interval", type=int, default=10)
parser.add_argument("--resize", type=int, default=None, nargs="+")
opt = parser.parse_args()
def video2jpg():
if not os.path.exists(opt.save_dir):
os.makedirs(opt.save_dir)
cap = cv2.VideoCapture(opt.video_path)
l_cap = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
for i in range(l_cap):
flag, frame = cap.read()
if flag == True and i % opt.interval == 0:
save_path = os.path.join(opt.save_dir, "{}_{}.jpg ".format(os.path.basename(opt.video_path).split(".")[0], i))
if opt.resize != None:
frame = cv2.resize(frame, (opt.resize[0], opt.resize[1]))
cv2.imwrite(save_path, frame)
print("{} has been saved".format(save_path))
if __name__ == "__main__":
video2jpg()
For example, if you want to resize douga.avi, which exists in the same directory as video2jpg.py, to 480 x 480 every 10 frames and save it as an image file, enter the following in the terminal.
python3 video2jpg.py --video_path douga.avi --interval 10 --resize 480 480
Recommended Posts