你需要一个模式"L;16"
;但是看起来 PIL"L"
在加载 PGM 时具有硬编码到 File.c 中的模式。如果您希望能够读取 16 位 PGM,则必须编写自己的解码器。
但是,16 位图像支持似乎仍然不稳定:
>>> im = Image.fromstring('I;16', (16, 16), '\xCA\xFE' * 256, 'raw', 'I;16')
>>> im.getcolors()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 866, in getcolors
return self.im.getcolors(maxcolors)
ValueError: image has wrong mode
我认为 PIL 能够读取16 位的图像,但实际上存储和操作它们仍然是实验性的。
>>> im = Image.fromstring('L', (16, 16), '\xCA\xFE' * 256, 'raw', 'L;16')
>>> im
<Image.Image image mode=L size=16x16 at 0x27B4440>
>>> im.getcolors()
[(256, 254)]
看,它只是将0xCAFE
值解释为0xFE
,这并不完全正确。