将 jpg 图像转换为 .hdf5

rma*_*iar 3 python jpeg image

在这里回答我自己的问题以供将来参考。

我最近正在处理.jpg图像类型的数据集,需要将它们转换为.hdf5图像。我看到了一些相反转换的答案,但没有从.jpgto .hdf5。做这个的最好方式是什么?

rma*_*iar 5

解决方案看起来像这样

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')
Run Code Online (Sandbox Code Playgroud)

我有一个工具可以在 https://github.com/raguiar2/jpg_to_h5上执行此操作