Search code examples
pythonimagejpeg

Convert jpg images to .hdf5


Answering my own question here for future reference.

I was recently working with a dataset of type .jpg images and needed to convert them to .hdf5 images. I saw some answers for converting the other way around, but nothing from .jpg to .hdf5. What is the best way to do this?


Solution

  • The solution looks like this

    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')
    

    I have a tool to do this at https://github.com/raguiar2/jpg_to_h5