将hdf5转换为文件夹中的原始组织 [英] Convert hdf5 to raw organised in folders

查看:102
本文介绍了将hdf5转换为文件夹中的原始组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用脚本使图像与图集匹配.此脚本输入是.raw个图像,这些图像组织在以下文件夹中:

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

我在hdf5中有一个图像,我想将其转换为多个文件,如前所述.这个组织看起来像hdf5,不是吗?

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?

我想知道是否可以在Python中执行此操作.如果是的话,我应该使用哪个包装? 我查看了h5py,但没有找到导出到raw并保留层次结构的函数.

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.

推荐答案

Feiten,您可以使用.visititems()递归调用函数(def)导出数据.您可以查询对象类型和名称.组名将是您的文件夹名,数据集名将是您的文件名.附件是一个非常简单的示例,显示了如何使用.visititems().如果您不熟悉h5py和/或HDF5结构,则它具有一些打印语句(注释掉),可输出更多信息.这应该可以帮助您入门.

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)    

这篇关于将hdf5转换为文件夹中的原始组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆