How to store image in hdf5 files with h5py?


#1

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.


#2

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?

Regards,
-Ken


#3

Thank you very much.