Use opencv to frame the video and save it

To use the cv2.imwrite() function, you need to import the cv2 library

#path为视频文件地址
cap = cv2.VideoCapture(path)

#vedio_save_path为图片分帧地址,注意不能写成path,后面要记得再加一个/
vedio_save_path='path/'

#用来给图片依次命名
c=0

while cap.isOpened():
    #success表示图片帧是否接收成功,image表示每一帧图片
    success, image = cap.read()
    if not success:
        print("Ignoring empty camera frame.")
        continue

    #保存每一帧图片(第一个参数为保存地址,第二个为image)
    cv2.imwrite(vedio_save_path + str(c) + '.jpg', image) 
    c+=1

    # 展示每一帧图片(window_name为字符串,可以自己设置)
    cv2.imshow('window_name', image)

    # 窗口按q退出
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

If you are not very clear about the basic operation functions of cv2, you can read these basic function introductions

1. Image processing function for CV library interface operation

2. Video processing function for CV library interface operation

 

Guess you like

Origin blog.csdn.net/weixin_63676550/article/details/129755572