7

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!!

5
  • Is the raster image a numpy array? Is the data an array as well? Reread the h5py documentation. I believe the numpy array is the basic unit of data that you can add with that package. Commented Apr 19, 2015 at 2:27
  • 1
    docs.h5py.org/en/latest/high/dataset.html - create_dataset is the basic mechanism for adding numpy arrays to a group. Commented Apr 19, 2015 at 4:19
  • I can add an array of data as an m x n matrix, but how do I add it such that it will display as an image; such as the ones in here: link using h5py? Commented Apr 19, 2015 at 15:31
  • 1
    It's possible that h5py has not included this image API in its port. You might have to ask the developers: github.com/h5py/h5py Commented Apr 19, 2015 at 16:30
  • 2
    why not save the array as it is and then have separate matplotlib code that plots it as an image? Commented Apr 19, 2015 at 18:09

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Exactly this. No idea how I missed this earlier when looking through docs. Thank you very much!
@MojoJojo I don't use Matlab, however there is an answer on the Matlab Answers website. Hope that helps.
Thank you! This is an excellent answer. For completeness, here are the attributes I have used for a single channel, grayscale image: { 'CLASS': 'IMAGE', 'IMAGE_VERSION': '1.2', 'IMAGE_SUBCLASS': 'IMAGE_GRAYSCALE', 'IMAGE_MINMAXRANGE': np.array([0, 255], dtype=np.uint8), 'IMAGE_WHITE_IS_ZERO': 0, }
The links posted are deprecated. They should be updated to support.hdfgroup.org/HDF5/Tutor/h5image.html and support.hdfgroup.org/HDF5/doc/ADGuide/ImageSpec.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.