1. 获取单张图片并保存
import cv2
# 获取本地摄像头
video_capture = cv2.VideoCapture(1)
while True:
ret, frame = video_capture.read()
#生成摄像头窗口
cv2.imshow("capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
#img/test.jpg 图片存储目录及名称
cv2.imwrite('img/test.jpg',frame)
break
video_capture.release()
cv2.destroyAllWindows()
2. 获取多张图片并保存
import cv2
VideoCapture = cv2.VideoCapture(1)
i=1
while(1):
ret, frame = VideoCapture.read()
cv2.imshow("capture", frame)
# img/ 是图片存储目录
cv2.imwrite('img/'+str(i)+'.jpg',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
i+=1
VideoCapture.release()
cv2.destroyAllWindows()
网友评论