I have 16-bit PGM images that I am trying to read in Python. It seems (?) like PIL does not support this format?
import Image
im = Image.open('test.pgm')
im.show()
Shows roughly the image, but it isn't right. There are dark bands throughout and img is reported to have mode=L
. I think this is related to an early question I had about 16-bit TIFF files. Is 16-bit that rare that PIL just does not support it? Any advice how I can read 16-bit PGM files in Python, using PIL or another standard library, or home-grown code?
You need a mode of "L;16"
; however it looks like PIL has a mode of "L"
hardcoded into File.c when loading a PGM. You’d have to write your own decoder if you want to be able to read a 16-bit PGM.
However, 16-bit image support still seems flaky:
>>> 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
I think PIL is capable of reading images with 16 bits, but actually storing and manipulating them is still experimental.
>>> 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)]
See, it just interpreted the 0xCAFE
value as 0xFE
, which isn’t exactly correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With