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 "pixel"). 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()
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