python opencv 拍照
时间: 2023-10-11 11:14:29 浏览: 67
Python OpenCV拍照并保存为jpg文件
要使用Python的OpenCV模块进行拍照,可以按照以下步骤进行操作:
1. 导入必要的模块:
```python
import cv2
```
2. 打开摄像头:
```python
cap = cv2.VideoCapture(0)
```
3. 循环读取每一帧图像:
```python
ret, frame = cap.read()
```
4. 在图像上添加文本提示信息:
```python
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, 'Press "q" to capture and quit', (10, 30), font, 1, (0, 255, 0), 2, cv2.LINE_AA)
```
5. 显示图像:
```python
cv2.imshow('Camera', frame)
```
6. 捕捉键盘输入,按下 'q' 键保存图像并退出:
```python
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.imwrite('out.jpg', frame)
break
```
7. 释放资源并关闭窗口:
```python
cap.release()
cv2.destroyAllWindows()
```
阅读全文