将jpg图像转换为.hdf5

问题描述 投票:0回答:1

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

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

python image jpeg
1个回答
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这样做

© www.soinside.com 2019 - 2024. All rights reserved.