0

I have two datasets of .tif files. For data_1, I can convert the .tif images to arrays by using scipy.imread() and np.array. But for the other dataset data_2, when I apply the same functions, the .tif files are not converted to float arrays.

import h5py
import os
import numpy as np
from scipy import misc
import hickle as hkl

data_1 = np.array([misc.imread(dir1+"/"+file)
                   for subdir, dirs, files in os.walk(dir1)
                   for file in files if file[-4:] == '.tif'])

data_2 = np.array([misc.imread(dir2+"/"+file)
                   for subdir, dirs, files in os.walk(dir2)
                   for file in files if file[-4:] == '.tif'])

f = h5py.File('images.hkl','w')

f.create_dataset(name='data_1', data = data_1)
f.create_dataset(name='data_2', data = data_2)

How do I convert from TIFF to an array?

ali_m
  • 71,714
  • 23
  • 223
  • 298
esw12345
  • 91
  • 1
  • 1
  • 7
  • What are the `.dtype` attributes of `data_1` and `data_2`? Please show your imports (what is `misc`)? – ali_m Jun 19 '16 at 20:37
  • data_1 is a npfloat array and data_2 ends up remaining as an array of type 'object' even though they should be exactly the same. misc is scipy. Here are the imports: from scipy import misc import numpy as np import hickle as hkl import h5py import os – esw12345 Jun 19 '16 at 20:58
  • 2
    `np.object` arrays usually result from attempting to concatenate arrays with incompatible shapes, or with dtypes that can't be safely cast to a common numeric type. Have you tried printing the `.shape` and `.dtype` attributes for the arrays returned by your second list comprehension? – ali_m Jun 19 '16 at 21:10
  • 1
    +1 what Ali said. Another remark: you don't want to do it that way (letting numpy decide the types depending on the input). It's very important to have control of the data-types. Most of the tiffs will use non-floating-point values to describe the images, some might use floating-point. It's important to handle these possible differences. I would recommend **scikit-image** which is kind of a wrapper of numpy for images. It helps handling with this. Relevant part of the docs [here](http://scikit-image.org/docs/stable/user_guide/data_types.html). – sascha Jun 19 '16 at 23:13

0 Answers0