python opencv以.dat(.bin)格式输出图片
时间: 2024-05-04 11:17:44 浏览: 127
首先,可以使用OpenCV库中的imread函数读取图像文件,并将其存储为numpy数组:
```python
import cv2
import numpy as np
img = cv2.imread('image.jpg')
```
然后,可以使用numpy中的tofile函数将numpy数组保存为二进制文件:
```python
img.tofile('image.dat')
```
如果想要将图像保存为.bin文件,可以使用以下代码:
```python
with open('image.bin', 'wb') as f:
f.write(img.tobytes())
```
注意,在读取二进制文件时,需要指定numpy数组的dtype和shape信息:
```python
with open('image.bin', 'rb') as f:
img_data = np.frombuffer(f.read(), dtype=np.uint8)
img = img_data.reshape((height, width, channels))
```
阅读全文
相关推荐








