[Image-SIG] Patch: Read/write big-endian 16-bit TIFF data properly

Zachary Pincus zpincus at stanford.edu
Wed Nov 29 10:10:45 CET 2006


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



-------------- next part --------------
A non-text attachment was scrubbed...
Name: tiff-endian.patch
Type: application/octet-stream
Size: 8850 bytes
Desc: not available
Url : http://mail.python.org/pipermail/image-sig/attachments/20061129/b2fd916c/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: BigEndian.tif
Type: image/tiff
Size: 118 bytes
Desc: not available
Url : http://mail.python.org/pipermail/image-sig/attachments/20061129/b2fd916c/attachment.tif 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: LittleEndian.tif
Type: image/tiff
Size: 118 bytes
Desc: not available
Url : http://mail.python.org/pipermail/image-sig/attachments/20061129/b2fd916c/attachment-0001.tif 
-------------- next part --------------



More information about the Image-SIG mailing list