import h5py
f = h5py.File('the_file.h5', 'r')
one_data = f['key']
print(one_data.shape)
print(one_data.dtype)
print(one_data)
I use the code above to print the info. The print result is:
(320, 320, 3)
uint8
<HDF5 dataset "1458552843.750": shape (320, 320, 3), type "|u1">
To use HDF5, numpy needs to be imported. One important feature is that it can attach metaset to every data in the file thus provides powerful searching and accessing. Let's get started with installing HDF5 to the computer. As HDF5 works on numpy, we would need numpy installed in our machine too.
Creating HDF5 files The first step to creating a HDF5 file is to initialise it. It uses a very similar syntax to initialising a typical text file in numpy. The first argument provides the filename and location, the second the mode. We're writing the file, so we provide a w for write access.
import cv2
import numpy as np
import h5py
f = h5py.File('the_file.h5', 'r')
dset = f['key']
data = np.array(dset[:,:,:])
file = 'test.jpg'
cv2.imwrite(file, data)
The solution provided by jet works just fine, but has the drawback of needing to include OpenCV (cv2). In case you are not using OpenCV for anything else, it is a bit overkill to install/include it just for saving the file. Alternatively you can use imageio.imwrite
(doc) which has lighter footprint, e.g.:
import imageio
import numpy as np
import h5py
f = h5py.File('the_file.h5', 'r')
dset = f['key']
data = np.array(dset[:,:,:])
file = 'test.png' # or .jpg
imageio.imwrite(file, data)
Installing imageio is as simple as pip install imageio
.
Also, matplotlib.image.imsave
(doc) provides similar image saving functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With