Numpy.array Image File "I; 16"

I want to use TIFF images to efficiently store large arrays of measurement data. Setting them to mode = "I; 16" (corresponding to my 16-bit data range), they give 2 MB files (~ 1000 × 1000 pixels). It's good.

However, I am having trouble reinstalling them into arrays when it comes to analyzing them. For 32-bit data (-> "I"), the numpy.array command works fine. In the case of “I; 16”, the result is a 0D numpy array with TIFF as the record [0,0].

Is there any way to make this work? I would very much like to avoid the use of 32-bit images, since I do not need a range, and it doubles the required space on the hard disk (many and many of these measurements are planned)

+6
source share
3 answers

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 K. Golke (very fast):

 import tifffile fp = r'path\to\image\image.tif' with tifffile.TIFFfile(fp) as tif: data = tif.asarray() 
+6
source

You can use GDAL + Numpy / Scipy to read bitmaps with 16-bit channel data:

 import gdal tif = gdal.Open('path.tif') arr = tif.ReadAsArray() 
+3
source

Convert (ImageJ) TIFF to an array with 8-bit number

 im = numpy.array(Image.open('my.tiff')) n = (im / numpy.amax(im) * 255).astype(numpy.uint8) 
0
source

Source: https://habr.com/ru/post/898815/


All Articles