Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 726
Jakob Avatar asked Oct 07 '11 08:10

Jakob


2 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 by C. Gohlke (very fast):

import tifffile

fp = r'path\to\image\image.tif'

with tifffile.TIFFfile(fp) as tif:
    data = tif.asarray()
like image 98
otterb Avatar answered Sep 21 '22 12:09

otterb


You could use GDAL + Numpy/Scipy to read raster images with 16bit channel data:

import gdal
tif = gdal.Open('path.tif')
arr = tif.ReadAsArray()
like image 25
xyz-123 Avatar answered Sep 19 '22 12:09

xyz-123