美文网首页OpenCvIT在线课程
Python从摄像头获取数据并保存为图片源码

Python从摄像头获取数据并保存为图片源码

作者: 阿青小屋 | 来源:发表于2019-04-16 17:03 被阅读1次

    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()

    相关文章

      网友评论

        本文标题:Python从摄像头获取数据并保存为图片源码

        本文链接:https://www.haomeiwen.com/subject/yttiwqtx.html