Feitan
Feitan

Reputation: 84

Convert hdf5 to raw organised in folders

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.

Upvotes: 0

Views: 1535

Answers (1)

kcw78
kcw78

Reputation: 7996

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)    

Upvotes: 1

Related Questions