I came across this thread trying to save 16-bit TIFF images with PIL / numpy.
Versions: python 2.7.1 - numpy 1.6.1 - PIL 1.1.7
Here is a quick test I wrote. uint16 numpy array → converted to string → converted to a PIL image of type 'I; 16 '→ saved as 16-bit TIFF.
Opening an image in ImageJ shows the right horizontal gradient chart, and the image type is "Bits per pixel: 16 (unsigned)"
import Image import numpy data = numpy.zeros((1024,1024),numpy.uint16) h,w = data.shape for i in range(h): data[i,:] = numpy.arange(w) im = Image.fromstring('I;16',(w,h),data.tostring()) im.save('test_16bit.tif')
edit: Starting with version 1.1.7, PIL does not support writing compressed files, but pylibtiff does (lzw compression). Thus, the test code becomes (tested with pylibtiff 0.3):
import Image import numpy from libtiff import TIFFimage data = numpy.zeros((1024,1024),numpy.uint16) h,w = data.shape for i in range(w): data[:,i] = numpy.arange(h) tiff = TIFFimage(data, description='') tiff.write_file('test_16bit.tif', compression='lzw')
Please note: the test code has changed to create a vertical gradient, otherwise compression is not achieved (see warning: pylibtiff currently supports reading and writing images that are stored using TIFF bands).
EgorZ Jan 27 '12 at 10:09 2012-01-27 10:09
source share