我有一个16位签名的单频段.tiff . 使用以下代码将其转换为numpy数组时:

from PIL import Image    
import numpy

dem = r"C:\myDEM.tif" #dem for digital elevation model

image = Image.open(dem,"r")
array = numpy.array(image) #also tried numpy.asarray(image), same result

print(image.size)
print(array.shape)
print(array)

我得到以下结果:

enter image description here

这让我相信从图像到数组的转换不像我期望的那样,特别是因为打印数组会产生'TiffImageFile'对象 .

根据PIL的changelog (1.1.6)Working with TIFFS (import, export) in Python using numpy,上述方法应该有效 . 用户'jez'的How to convert a PIL Image into a numpy array?的评论之一也描述了这个问题 .

这里出了什么问题?

PIL.VERSION = 1.1.7

编辑:调用 image.show() 返回"ValueError: unrecognized mode" . 也许这与错误有关?

尝试使用浮点输入栅格而不是整数 . 结果相同 .

尝试使用未压缩的.tiff . 结果相同 .

numpy.array of an "i;16" Image file似乎是同一个问题

Python and 16 Bit Tiff似乎声称这是一个错误 .

解决方案

使用无符号16位输入“解决”了这个问题,尽管它更像是一种解决方法 .