Convert hdf5 to raw organised in folders

让人想犯罪 __ 提交于 2019-12-13 02:41:19

问题


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.


回答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)    


来源:https://stackoverflow.com/questions/56171323/convert-hdf5-to-raw-organised-in-folders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!