Search code examples
pythonopencv

Saving video frames with timestamps


I needed to save the image frame with the file name as the Timestamp. I tried to add timestamp by time.time() function in cv2.putText() function and It just writes time.time() .

Also I tried using time.time() in place of image name of cv2.imwrite() function and its not adding timestamp either.

font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,'time.time()',(10,500), font, 4,(255,255,255),2,cv2.CV_AA). 

                           
cv2.imwrite('str(time.time())'+str(i)+'.jpg',frame)
`
The issue is resolved by putting str(time.time()).

Solution

  • Currently you have time.time() in quotes so it literally sets the text as a string called "time.time()". To display the actual value of time.time(), remove the quotations. From the docs, cv2.putText() takes in a str for the 2nd parameter. Here's the definition

    cv2.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])
    

    So in your case, you can just turn the value of time.time() into a str type like this

    cv2.putText(frame, str(time.time()), (10,500), font, 4, (255,255,255), 2, cv2.CV_AA)
    

    Similarly with cv2.imwrite(), instead of the literal string, you can turn the value into a str type to get the timestamp

    cv2.imwrite(str(time.time()) + '_' + str(i) + '.jpg', frame)