python opencv打开摄像头,截图保存

python opencv打开摄像头,截图保存,学习结果如下:
使用python打开摄像头

import cv2
cap = cv2.VideoCapture(0) #设置摄像头 0是默认的摄像头 如果你有多个摄像头的话呢,可以设置1,2,3....
while True:   #进入无限循环
    ret,frame = cap.read() #将摄像头拍到的图像作为frame值
    frame = cv2.flip(frame, 1)  # 摄像头是和人对立的,将图像左右调换回来正常显示。
    cv2.imshow('video',frame) #将frame的值显示出来 有两个参数 前一个是窗口名字,后面是值
    c = cv2.waitKey(1) #判断退出的条件 当按下'Q'键的时候呢,就退出
    if c == ord('q'):
        break
cap.release()  #常规操作
cv2.DestroyAllWindows()

截图保存

import cv2
cap = cv2.VideoCapture(0) #设置摄像头 0是默认的摄像头 如果你有多个摄像头的话呢,可以设置1,2,3....
while True:   #进入无限循环
    ret,frame = cap.read() #将摄像头拍到的图像作为frame值
    frame = cv2.flip(frame, 1)  # 摄像头是和人对立的,将图像左右调换回来正常显示。
    cv2.imshow('video',frame) #将frame的值显示出来 有两个参数 前一个是窗口名字,后面是值
    c = cv2.waitKey(1) #判断退出的条件 当按下'Q'键的时候呢,就退出
    if c == ord('q'):  # 如果按下q 就截图保存并退出
        saveFile = "G:\python picture\测试图02.jpg"  # 带有中文的保存文件路径
        # cv2.imwrite(saveFile, img3)  # imwrite 不支持中文路径和文件名,读取失败,但不会报错!
        img_write = cv2.imencode(".jpg", frame)[1].tofile(saveFile)
        break
cap.release()  #常规操作
cv2.destroyAllWindows()

报错:
1、IndentationError: unindent does not match any outer indentation level

F:\下载\python\python.exe "G:/新建文件夹 (2)/python编程/turn on the camera.py"
  File "G:/新建文件夹 (2)/python编程/turn on the camera.py", line 6
    frame = cv2.flip(frame, 1)  # 摄像头是和人对立的,将图像左右调换回来正常显示。
                                                          ^
IndentationError: unindent does not match any outer indentation level

报错原因:table与空格混用,导致代码没有对齐。

2、IndentationError: expected an indented block

F:\下载\python\python.exe "G:/新建文件夹 (2)/python编程/turn on the camera.py"
  File "G:/新建文件夹 (2)/python编程/turn on the camera.py", line 4
    ret,frame = cap.read() #将摄像头拍到的图像作为frame值
    ^
IndentationError: expected an indented block

报错原因:没有缩进。python使用缩进来表示上下级,从属关系,例如for循环。

参考链接:
1、Python调用摄像头
2、利用python打开电脑摄像头
3、python编程出现:expected an indented block错误。
4、Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法
5、【OpenCV 例程300篇】02. 图像的保存(cv2.imwrite)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值