3
$\begingroup$

I am using opencv module in python to extract sequences of images(frames) from video (from a file) My code for extracting images from video goes like this.

import cv2 import os os.chdir('D:/Python images/videofolder') cap = cv2.VideoCapture('D:/Python images/test_videos/testcutvideo1.avi') cap.isOpened() success,image = cap.read() count = 1 success = True while success: success,image = cap.read() cv2.imwrite("image%d.jpg" % count, image) # save frame as JPEG file count += 1 

I want my code to select multiple of 'n' images eg: to select & save multiple of 5 images(to extract multiple of 5) so that I have 5th, 10th, 15th... images. Please guide me.

$\endgroup$

1 Answer 1

1
$\begingroup$

You can use the modulo operator to identify multiples of count. Whenever it is a multiple of 5 save the image, otherwise just disregard it and move to the next iteration of the while loop.

Make sure the counter is outside the if statement. It will count every iteration. That is how we will determine if the current image is a multiple of 5.

import cv2 import os os.chdir('D:/Python images/videofolder') cap = cv2.VideoCapture('D:/Python images/test_videos/testcutvideo1.avi') cap.isOpened() count = 0 success = True while success: success,image = cap.read() if count % 5 == 0: cv2.imwrite("image%d.jpg" % count, image) # save frame as JPEG file count += 1 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.