Python_opencv库实现调用摄像头拍照并保存到本地【电脑随时拍】

原文链接:https://blog.csdn.net/qq_42069296/article/details/121401327

导入 opencv-python库,复制代码即可运行

import cv2
 
def picture_shoot(image_name='img.png', image_path=r'E:/') -> None:
    '''
    调用摄像头拍照并保存图片到本地
    :param image_name: 图片名字
    :param image_path: 图片保存路径
    :return: None
    '''
    cap = cv2.VideoCapture(0)
    while (cap.isOpened()):
        ret, frame = cap.read()
        # cv2.imshow("Capture_Paizhao", frame) # 显示窗口
        cv2.imwrite(image_path + "\\" + image_name, frame)
        print("保存" + image_name + "成功!")
        break
    cap.release()
    cv2.destroyAllWindows()
 
 
if __name__ == '__main__':
    picture_shoot()

运行结果:

D:\Python\python.exe D:/PythonWorkSpace/chenbang/test.py
保存img.png成功!
[ WARN:0] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (438) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Process finished with exit code 0

应用场景:UI自动化需要对比实际图片和期望图片时,可使用该方法获取实际图片

注意事项:# cv2.imshow(“Capture_Paizhao”, frame) # 显示窗口,因为拍图过程太快,窗口还没来得及显示就会关掉。

Python:3.10.0

opencv:4.5.4.58

你可能感兴趣的:(python,人工智能,python,opencv)