Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I visualise an image in h5 format data?

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">
like image 744
DunkOnly Avatar asked Jan 13 '17 03:01

DunkOnly


People also ask

How do I open a H5 file in Python?

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.

How do I create a h5py file?

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.


2 Answers

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)
like image 111
DunkOnly Avatar answered Oct 23 '22 09:10

DunkOnly


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.

like image 40
MF.OX Avatar answered Oct 23 '22 09:10

MF.OX