在这里回答我自己的问题以供将来参考。
我最近使用的是.jpg
图像类型的数据集,需要将它们转换为.hdf5
图像。我看到了转换相反方式的一些答案,但没有从.jpg
到.hdf5
。做这个的最好方式是什么?
解决方案看起来像这样
def convert_file(input_dir, filename, output_dir):
filepath = input_dir + '/' + filename
fin = open(filepath, 'rb')
binary_data = fin.read()
new_filepath = output_dir + '/' + filename[:-4] + '.hdf5'
f = h5py.File(new_filepath)
dt = h5py.special_dtype(vlen=np.dtype('uint8'))
dset = f.create_dataset('binary_data', (100, ), dtype=dt)
dset[0] = np.fromstring(binary_data, dtype='uint8')
我有一个工具在https://github.com/raguiar2/jpg_to_h5这样做