使用OpenCV Python保存带有时间戳的图像流

2024-09-30 12:34:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用opencv,Python来保存jpg和png格式的相同相机图像。 我使用时间戳按顺序保存图像。下面是我的代码示例。但问题是每次执行时它只保存一个图像。保存带有时间戳的图像流的最佳解决方案是什么

import numpy as np
import cv2
import time

camera = cv2.VideoCapture(0)
time = time.time() #timestamp

def saveJpgImage(frame):
    #process image
    img_name = "opencv_frame_{}.jpg".format(time)
    cv2.imwrite(img_name, frame)

def savePngImage():
    #process image
    img_name = "opencv_frame_{}.png".format(time)
    cv2.imwrite(img_name, frame)

def main():
    while True:
        ret, frame = cam.read()
        cv2.imshow("Camera Images", frame)
        if not ret:
            break
        k = cv2.waitKey(1)

        if k%256 == 27:
            # ESC pressed
            print("Escape hit, closing...")
            break
        elif k%256 == 32:
            saveJpgImage(frame)
            savePngImage(frame)


if __name__ == '__main__':
    main()

Tags: name图像importimgiftimepngmain

热门问题