Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does h5py support image datasets? #771

Closed
felix1028 opened this issue Nov 3, 2016 · 8 comments
Closed

Does h5py support image datasets? #771

felix1028 opened this issue Nov 3, 2016 · 8 comments

Comments

@felix1028
Copy link

I need to add a png/jpg image to my hdf5 file. I tried using create_dataset("image", data='path/to/filefig.png', dtype='uint8') and get this error:

IOError: Can't prepare for writing data (No appropriate function for conversion path)

Is appending an image file to my hdf5 file possible?
Thanks,
-Mariana

@felix1028
Copy link
Author

BTW, I'm looking to do a truecolor image, instead of a indexed image.

@pdebuyl
Copy link
Contributor

pdebuyl commented Nov 5, 2016

The data argument is interpreted as a string and the dtype argument requests a conversion to an unsigned integer. If you load your image as a NumPy array first, via whatever method your prefer, you should be fine.

@felix1028
Copy link
Author

Thanks for the info! I think I'm most of the way there.

I'm making a plot with MatplotLib and saving it as a PNG so I can use the Image library to open the PNG file as a NumPy ndarray 'int8'. I can pass this 3d array (800,1200,4) to my HDF5 file using h5py's create_dataset call. This plus a few attributes (CLASS=IMAGE, IMAGE_VERSION='1.2', IMAGE_SUBCLASS=IMAGE_TRUECOLOR, INTERLACE_MODE=INTERLACE_PIXEL) puts the data in my hdf5 file without errors.

However, this data comes out as a table in my hdf5 file. I can make this show up as an image after some selections with HDFView, but everything defaults to a table.

Would you happen to know if there are any other requirements to have the image data show up as an image, instead of a table?

@pdebuyl
Copy link
Contributor

pdebuyl commented Nov 8, 2016

I am not using images myself. I don't know how HDFView is suppose to behave in such circumstances.

The attributes you have given correspond to the specification in https://support.hdfgroup.org/HDF5/doc/ADGuide/ImageSpec.html so I see no reason for it not to work. If you post sample code, I am willing to test it on my system.

Also, as you have a PNG image, you might consider to store it "as is", as a binary stream instead of a full raster image.

@felix1028
Copy link
Author

felix1028 commented Nov 10, 2016

Here is the idea: I have a set of latitude and longitude points that I need to save in an image in an hdf5 file. This is a simplified example of what I want to do that shows the problem I'm having:

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
import numpy as np
import h5py

figure = plt.figure(dpi=200)
lons=np.array([160.0, 23.0, 120.5, 65.1])
lats=np.array([-10, 0 , 40, -30])
maps = Basemap(projection='cyl',llcrnrlat=-60,urcrnrlat=60,llcrnrlon=-180,urcrnrlon=180)
x, y = maps(lons, lats)
maps.plot(x,y, '.', color='black', markersize=8)

#Draw the image
figure.canvas.draw()
#Now we can save it to a numpy array.
idata = np.fromstring(figure.canvas.tostring_rgb(), dtype=np.uint8, sep='')
idata = idata.reshape(figure.canvas.get_width_height()[::-1] + (3,))

hf = h5py.File('/home/usr/imageSample.HDF5', 'w')

dset=hf.create_dataset("/myData/myImage", data=idata, dtype='uint8', chunks=True)
#Set the image attributes
dset.attrs['CLASS'] = 'IMAGE'
dset.attrs['IMAGE_VERSION'] = '1.2'
dset.attrs['IMAGE_SUBCLASS'] = 'IMAGE_TRUECOLOR'
dset.attrs['INTERLACE_MODE'] = 'INTERLACE_PIXEL'
dset.attrs['IMAGE_COLORMODEL'] = 'RGB'

hf.close()

@felix1028
Copy link
Author

Just figured out. Its something to do with the attribute additions in the end. The correct calls are:
dset.attrs.create('CLASS', 'IMAGE')
dset.attrs.create('IMAGE_VERSION', '1.2')
dset.attrs.create('IMAGE_SUBCLASS', 'IMAGE_TRUECOLOR')
dset.attrs.create('INTERLACE_MODE', 'INTERLACE_PIXEL')

Thank you for your help!
-Mariana

@pdebuyl
Copy link
Contributor

pdebuyl commented Nov 13, 2016

Hi,

Thanks for the update with the fix! For info, I checked the earlier syntax but defining only the 4 attributes set in the update, i.e.

dset.attrs['CLASS'] = 'IMAGE'
dset.attrs['IMAGE_VERSION'] = '1.2'
dset.attrs['IMAGE_SUBCLASS'] = 'IMAGE_TRUECOLOR'
dset.attrs['INTERLACE_MODE'] = 'INTERLACE_PIXEL'

and it works as well (hdfview shows the image properly).

If your problem is solved, could you close the issue?

Regards,

Pierre

@felix1028
Copy link
Author

Pierre,
thanks for the follow-up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants