numpy.array of an "I;16" Image file
I want to use TIFF images to effectively save large arrays of measurement data. With setting them to mode="I;16" (corresponding to my 16 bit data range), they yield 2MB files (~1000x1000 "pi开发者_JAVA百科xel"). Which is good.
However I am having troubles reconverting them into arrays when it comes to analysing them. For 32bit data (-> "I") the numpy.array command works fine. In case of "I;16" the result is a 0D numpy array with the TIFF as the [0,0] entry.
Is there a way to get that to work? I would really like to avoid using 32bit images, as I don't need the range and it doubles the HDD space required (lots and lots of those measurements planned...)
This should work (pillow/PIL solution, slow for 16-bit image, see below).
from PIL import Image
import numpy as np
data = np.random.randint(0,2**16-1,(1000,1000))
im = Image.fromarray(data)
im.save('test.tif')
im2 = Image.open('test.tif')
data2 = np.array(im2.getdata()).reshape(im2.size[::-1])
Another solution using tifffile by C. Gohlke (very fast):
import tifffile
fp = r'path\to\image\image.tif'
with tifffile.TIFFfile(fp) as tif:
data = tif.asarray()
You could use GDAL + Numpy/Scipy to read raster images with 16bit channel data:
import gdal
tif = gdal.Open('path.tif')
arr = tif.ReadAsArray()
Convert an (ImageJ) TIFF to an 8 bit numpy array
im = numpy.array(Image.open('my.tiff'))
n = (im / numpy.amax(im) * 255).astype(numpy.uint8)
精彩评论