enviclass将BIN格式图片转成bmp格式
时间: 2023-07-09 15:35:58 浏览: 60
下面是一个Python的enviclass示例代码,将高光谱BIN格式图片转换为BMP格式:
```python
import numpy as np
from PIL import Image
# 读取BIN格式图片
with open('input.bin', 'rb') as f:
cols, rows, bands = 512, 512, 224
img_bin = np.fromfile(f, dtype=np.float32, count=cols * rows * bands)
img_bin = img_bin.reshape(cols, rows, bands)
# 将图像转换为16位整数格式
img_bmp = np.uint16(img_bin / np.max(img_bin) * 65535)
# 创建空的BMP图像
img_bmp = Image.fromarray(np.zeros((rows, cols), dtype=np.uint16))
# 将BMP图像的每个像素赋值为对应的16位整数值
for band in range(bands):
img_band = Image.fromarray(img_bmp[:, :, band])
img_band.putdata(list(img_bmp[:, :, band].flatten()))
img_bmp[:, :, band] = np.array(img_band).reshape(rows, cols)
# 将图像保存为BMP格式
img_bmp.save('output.bmp')
```
在上述代码中,使用```numpy```库和```PIL```库读取BIN格式图片,并将其转换为16位整数格式。然后,创建一个空的BMP图像,并将每个像素的值赋值为对应的16位整数值。最后,使用```save```方法将图像保存为BMP格式。