python - 如何从多波段 tif 文件生成原始文件?

标签 python python-imaging-library gdal

我尝试打开每像素 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/

相关文章:

gdal - 使用 ECW 支持安装 GDAL

python - 将 python 代码迁移到 pypy 或更多关于 future 可靠的东西

python - 如何在 Django API 中显示结果

python - 使用 GDAL/OGR python 模块解析 osm.pbf 数据

Python:如何知道PIL中的边界框有多少种颜色?

python - 如何在Pillow中的Python中将24位深度更改为8位深度?

python - Django 迁移找不到 GDALRaster

python - 在组合网络的子网上使用两种损失

python - multiprocessing.Pool,不会永久影响类变量

python - 使用 PIL 将图像转换为特定调色板而不抖动