将jpg图像转换为.hdf5

时间:2019-05-01 22:09:29

标签: python image jpeg

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

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

2 个答案:

答案 0 :(得分:0)

解决方案如下

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

答案 1 :(得分:0)

我还创建了自己的迷你库来处理h5文件。这是链接: https://github.com/ss4328/h5_manager_scripts

该脚本负责从目录创建h5,合并h5以及可视化h5中的12张图像。