How to store image in hdf5 files with h5py?


Hello everyone:
I read the image file with matplotlib and stored the array in dataset, but hdfviewer cannot recognized the dataset as image object.
I desired to know if there is any way to store the image in hdf5 easily and if there is any function store image in h5py?
Thanks in advance.


How many images do you want to import? If only a few, HDF View can read images into dataset objects. Look under Tools/Convert Images To > HDF5 or HDF4, then enter the image and HDF5 file names.

If you want to import “a lot” of files, you can do it with h5py or PyTables, but will also need a Python package to read the image files and convert to NumPy arrays. Two popular packages are OpenCV (cv2) and Pillow. I have posted answers on StackOverflow showing how to use each of these. See the following links for examples:

with OpenCV:
Convert a folder comprising jpeg images to hdf5
with Pillow:
How do I process a large dataset of images in python?

These methods store converted images as raw array data. You will not be able to view the image in HDF View unless you add appropriate attributes. See this answer for details:
How to save a pyplot image as .h5 file in python and visualize with HDFView?



Thank you very much.


But how to convert h5 format images to jpg/png format??
Thanks in advance.


But how to convert h5 format images to jpg/png format.
Thank you in advance.


You’ll need to use some kind of format-specific encoder to format a given pixel array. See for example: Convert a NumPy array to an image