我尝试打开每像素 16 位和多波段的 tif 图像,将其转换为原始文件。我在接下来的命令 i = Image.open('image.tif')
和使用 rawData = i.tostring()
之后使用 PIL。它不适用于多波段 tif 图像。
错误是:
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
该目录包含该文件。
我必须怎么做?
最佳答案
GDAL 非常擅长打开多波段栅格,并且支持 11 different band types ,包括 int16。
from osgeo import gdal
import numpy as np
ds = gdal.Open('image.tif')
# loop through each band
for bi in range(ds.RasterCount):
band = ds.GetRasterBand(bi + 1)
# Read this band into a 2D NumPy array
ar = band.ReadAsArray()
print('Band %d has type %s'%(bi + 1, ar.dtype))
raw = ar.tostring()
关于python - 如何从多波段 tif 文件生成原始文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12278653/