4

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()
b0r.py
  • 71
  • 1
  • 7
  • 1
    If the `img.show()` shows a color image, based on data it loaded from the `hdf` file, then color has been saved. If `HDFView`only shows a greyscale, then that's a problem with `HDFView` (or its settings), not a problem with the file save. – hpaulj Aug 30 '18 at 19:21

1 Answers1

3

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 package

import h5py  
import numpy as np
from PIL import Image

hdf = h5py.File("Sample.h5",'w')
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'] = np.string_('IMAGE')
dset.attrs['IMAGE_VERSION'] = np.string_('1.2')
arr = np.asarray([0, 255], dtype=np.uint8)
dset.attrs['IMAGE_MINMAXRANGE'] = list(arr)
dset.attrs['IMAGE_SUBCLASS'] = np.string_('IMAGE_TRUECOLOR')
dset.attrs['INTERLACE_MODE'] = np.string_('INTERLACE_PIXEL')
hdf.close()

This gives in HDFview by double clicking on dataset "Image 1" my image stored h5 style !

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

In your case it's RBG

Therefore

import h5py
import numpy as np
from PIL import Image

hdf = h5py.File("Sample.h5",'r')
array = np.array(list(hdf.get("Photos/Image 1")))
img = Image.fromarray(array.astype('uint8'), 'RGB')
img.show()

will give you

my deserialized picture

PilouPili
  • 2,601
  • 2
  • 17
  • 31