Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add raster image to HDF5 file using h5py

I apologize if this is sort of a newbie question, but I am fairly new to Python and HDF5. I am using h5py, numpy, and Python 2.7. I have data from various files that need to be imported into one HDF5 file. The data from each file is to be stored in a different group. Each of these groups needs to contain 1) the raw data from the file as an m x n matrix and 2) an image raster generated from normalized raw data.

I am able to accomplish part 1, and am able to normalize the data, but I am not able to write this normalized data to a raster image because I don't know how to add a raster image to a group. It seems like there should be an easy, straightforward way to do this, but I have read the documentation and haven't found one. How would one do this in h5py, and if it can't be done using h5py, what should I use to accomplish this?

Thanks!!

like image 951
pyguy Avatar asked Apr 19 '15 00:04

pyguy


1 Answers

There is nothing special about images in HDF5. The link you provided is for the high level library bindings. You can just as easily use the specifications of images 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 then look at the image in HDFView:

Image within HDF5 file

Note that you have to open the image data with "Open As" in order to see it as an image, as the table view is the default.

like image 159
Timothy Brown Avatar answered Oct 21 '22 16:10

Timothy Brown