我使用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()
当按键按下时测试,按下键时调用save函数。如果您想在按键时调用视频循环,请这样做!(别忘了包括escape方法!)在
相关问题 更多 >
编程相关推荐