Add bitmap to HDF5 file using h5py

Sorry if this is a newbie question, but I'm pretty new to Python and HDF5. I am using h5py, numpy and Python 2.7. I have data from different files that need to be imported into a single HDF5 file. Data from each file must be stored in a different group. Each of these groups should contain 1) raw data from a file in the form of an mxn matrix, and 2) a raster image generated from normalized source data.

I can perform part 1 and can normalize the data, but I cannot write this normalized data to a bitmap, because I do not know how to add a bitmap to a group. There seems to be a simple and straightforward way to do this, but I read the documentation and did not find it. How to do it in h5py, and if it is not possible to do it with h5py, what should I use for this?

Thanks!!

+6
source share
1 answer

There is nothing special about images in HDF5. The link provided is for high-level library bindings. You can also easily use image specifications in HDF5, which are just attributes.

Here is a very quick and dirty example:

#!/usr/bin/env python import numpy as np import h5py # Define a color palette pal = np.array([[0, 0, 168], [0, 0, 252], [0, 168, 252], [84, 252, 252], [168, 252, 168], [0, 252, 168], [252, 252, 84], [252, 168, 0], [252, 0, 0]], dtype=np.uint8 ) # Generate some data/image x = np.linspace(0,pal.shape[0]-1) data,Y = np.meshgrid(x,x) # Create the HDF5 file f = h5py.File('test.h5', 'w') # Create the image and palette dataspaces dset = f.create_dataset('img', data=data) pset = f.create_dataset('palette', data=pal) # Set the image attributes dset.attrs['CLASS'] = 'IMAGE' dset.attrs['IMAGE_VERSION'] = '1.2' dset.attrs['IMAGE_SUBCLASS'] = 'IMAGE_INDEXED' dset.attrs['IMAGE_MINMAXRANGE'] = np.array([0,255], dtype=np.uint8) dset.attrs['PALETTE'] = pset.ref # Set the palette attributes pset.attrs['CLASS'] = 'PALETTE' pset.attrs['PAL_VERSION'] = '1.2' pset.attrs['PAL_COLORMODEL'] = 'RGB' pset.attrs['PAL_TYPE'] = 'STANDARD8' # Close the file f.close() 

Run the example and look at the image in HDFView:

Image within HDF5 file

Note that you must open the image data with "Open As" to see it as an image, since the table view is used by default.

+7
source

Source: https://habr.com/ru/post/985444/


All Articles