0

I use a script to make images match with an atlas. This script input is .raw images organised in folders like:

imageFolder
-- folder1
---- image1.raw
---- image2.raw
-- folder2
---- image1.raw
---- image2.raw

I have an image in hdf5 and I would like to convert it into multiple files as presented before. This organization looks like hdf5, doesn't it?

I would like to know if it's possible to do this in Python. And if it is, which package should I use? I looked at h5py but I didn't find a function to export to raw and keep the hierarchy.

Amir Shabani
  • 3,857
  • 6
  • 30
  • 67
Feitan
  • 84
  • 6
  • Please clarify. Are you trying to import the `imageX.raw` files into HDF5 and maintain the folder/file hierarchy? Or do you want to export each`imageX.raw` dataset from HDF5 and maintain the folder/file hierarchy? You can do both. It's just not clear which direction you want to go. – kcw78 May 16 '19 at 17:01
  • Hi @kcw78 , thank you for your answer. I want to export each imageX.raw from HDF5 and maintain the hierarchy. – Feitan May 16 '19 at 17:37

1 Answers1

1

Feiten, you can use .visititems() to recursively call a function (def) to export the data. You can query the object type and name. Group names will be your folder names and Dataset names will be your file names. Attached is a very simple example that shows how to use .visititems(). It has some print statements (commented out) that output more info if you are unfamiliar with h5py and/or HDF5 structure. This should get you started.

import h5py

def print_grp_name(grp_name, object):

#  print ('object = ' , object)
#  print ('Group =', object.name)

  try:
    n_subgroups = len(object.keys())
    #print ('Object is a Group')
  except:
    n_subgroups = 0
    #print ('Object is a Dataset')
    dataset_list.append (object.name)

#  print ('# of subgroups = ', n_subgroups )

if __name__ ==  '__main__' :  
    with h5py.File(your-filename-here,'r') as h5f:

        print ('visting group = ', h5f)
        dataset_list = []
        h5f.visititems(print_grp_name)

    print (dataset_list)    
kcw78
  • 7,131
  • 3
  • 12
  • 44
  • Thank you for this detailed example. I managed to convert it to **.raw** and kept the hierarchy. – Feitan May 21 '19 at 21:08
  • The code above tests the number of object keys to determine if the object is a group (when keys>0) or dataset (when keys=0). IMHO, it is more Pythonic to use `isinstance()` as the test. See this answer (to a similar question) for an example using `isinstance()`. (https://stackoverflow.com/a/57067674/10462884) – kcw78 Sep 02 '19 at 22:20