Hello all,

I've found and fixed a minor problem in PIL's TIFF handling.

SUMMARY: PIL does not read 16-bit TIFF files correctly if they are in big-endian byte order. A patch to fix this is included.

MORE DETAIL:
16-bit grayscale TIFF files are a (very) common scientific imaging format. Unfortunately, PIL does not read these files gracefully. Specifically, while PIL's TiffImagePlugin can properly read big- and little-endian TIFF tag directories, the plugin does not pass on information about the endian-ness of the file to the decoder, so the actual image bytes are read incorrectly.

Attached is a patch to fix this problem. The patch keys the image mode and raw mode on the endian-ness of the input file, as well as the rest of the TIFF tags, so that these modes are properly passed to the decoder. Additionally, the patch allows for proper writing of big- endian tiff files.

Also attached are two 2x2 16-bit TIFF images, with all pixels having value 256. One image is little-endian and the other big-endian. Here's a test case for the behavior, with these images:
Current PIL 1.1.6b2:
In [1]: import Image
In [2]: im = Image.open('BigEndian.tif')
In [3]: im.getpixel((0,0))
Out[3]: 1
In [4]: im = Image.open('LittleEndian.tif')
In [5]: im.getpixel((0,0))
Out[5]: 256

Patched PIL 1.1.6b2:
In [1]: import Image
In [2]: im = Image.open('BigEndian.tif')
In [3]: im.getpixel((0,0))
Out[3]: 256
In [4]: im = Image.open('LittleEndian.tif')
In [5]: im.getpixel((0,0))
Out[5]: 256

Zach Pincus

Program in Biomedical Informatics and Department of Biochemistry
Stanford University School of Medicine



Attachment: tiff-endian.patch
Description: Binary data

TIFF image

TIFF image


_______________________________________________
Image-SIG maillist  -  Image-SIG@python.org
http://mail.python.org/mailman/listinfo/image-sig

Reply via email to