Created
September 7, 2014 15:13
-
-
Save RodrigoPrior/e3ffd7c7dd7184cb0448 to your computer and use it in GitHub Desktop.
Create a dataset respecting the HDF5 image specification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# images to hdf5 | |
# https://groups.google.com/forum/#!topic/h5py/9sA18XbC_ao | |
# https://github.com/h5py/h5py/issues/268 | |
import h5py | |
import numpy | |
def create_image_dataset(group, dataset_name, image, **kwargs): | |
""" | |
Create a dataset respecting the HDF5 image specification | |
http://www.hdfgroup.org/HDF5/doc/ADGuide/ImageSpec.html | |
group (HDF group): the group that will contain the dataset | |
dataset_name (string): name of the dataset | |
image (numpy.ndimage): the image to create. It should have at least 2 dimensions | |
returns the new dataset | |
""" | |
assert(len(image.shape) >= 2) | |
image_dataset = group.create_dataset(dataset_name, data=image, **kwargs) | |
# numpy.string_ is to force fixed-length string (necessary for compatibility) | |
image_dataset.attrs["CLASS"] = numpy.string_("IMAGE") | |
# Colour image? | |
if len(image.shape) == 3 and (image.shape[0] == 3 or image.shape[2] == 3): | |
# TODO: check dtype is int? | |
image_dataset.attrs["IMAGE_SUBCLASS"] = numpy.string_("IMAGE_TRUECOLOR") | |
image_dataset.attrs["IMAGE_COLORMODEL"] = numpy.string_("RGB") | |
if image.shape[0] == 3: | |
# Stored as [pixel components][height][width] | |
image_dataset.attrs["INTERLACE_MODE"] = numpy.string_("INTERLACE_PLANE") | |
else: # This is the numpy standard | |
# Stored as [height][width][pixel components] | |
image_dataset.attrs["INTERLACE_MODE"] = numpy.string_("INTERLACE_PIXEL") | |
else: | |
image_dataset.attrs["IMAGE_SUBCLASS"] = numpy.string_("IMAGE_GRAYSCALE") | |
image_dataset.attrs["IMAGE_WHITE_IS_ZERO"] = numpy.array(0, dtype="uint8") | |
image_dataset.attrs["IMAGE_MINMAXRANGE"] = [image.min(), image.max()] | |
image_dataset.attrs["DISPLAY_ORIGIN"] = numpy.string_("UL") # not rotated | |
image_dataset.attrs["IMAGE_VERSION"] = numpy.string_("1.2") | |
return image_dataset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment