Im currently working on a program that converts text files and jpg-images into the HDF5-Format. Opened with the HDFView 3.0, it seems that the Images are only saved in greyscales.
hdf = h5py.File("Sample.h5")
img = Image.open("Image.jpg")
data = np.asarray((img), dtype="uint8")
hdf.create_dataset("Photos/Image 1", data=data, dtype='uint8')
dset = hdf.get("Photos/Image 1")
dset.attrs['CLASS'] = 'IMAGE'
dset.attrs['IMAGE_VERSION'] = '1.2'
arr = np.asarray([0, 255], dtype=np.uint8)
dset.attrs['IMAGE_MINMAXRANGE'] = list(arr)
dset.attrs['IMAGE_SUBCLASS'] = 'IMAGE_TRUECOLOR'
dset.attrs['INTERLACE_MODE'] = 'INTERLACE_PIXEL'
In python it is possible to show the original colored image with the Image.show() method:
hdf = h5py.File("Sample.h5")
array = np.array(list(hdf.get("Photos/Image 1")))
img = Image.fromarray(array.astype('uint8'))
img.show()
First part of the question.
Don't ask me why but maybe one of the maintainers of HDFview can step up. To enable HDFview to correctly display images the attributes must be finite length string to be correctly interpreted.
Use
np.string_(<string>)
from numpy packageThis gives in HDFview by double clicking on dataset "Image 1"
Second question.
I suppose you are using the PIL package The function
fromarray
expects the "mode of the image" see https://pillow.readthedocs.io/en/3.1.x/handbook/concepts.html#concept-modesIn your case it's RBG
Therefore
will give you