Working with hdf5 files in VTK python
I have a 3D image stack, saved on disk as a set of individual 2D image files. I have around 1000 such image files, each about 10 MB, so the total datset is about 10 GB.
I'm currently using the vtkPNGReader class to read these images. I rename my 2D image files using a consistent pattern, so that I can use the SetFilePattern method in that class and the appropriate image is read in.
I want to move this workflow to a better data structure, preferably hdf5. I can use the h5py library to convert the individual 2D image files into a single large hdf5 file, but I'm not sure how to read it into VTK. I couldn't find any examples for this class vtkHDFReader. If there is a better file type for this, please let me know.
My current png reader code:
reader = vtk.vtkPNGReader()
reader.SetFilePrefix(imagesPath + '\\')
reader.SetFilePattern('%sImage_%05d.png')
reader.SetDataExtent(0, self.extent_x, 0, self.extent_y, 0, self.extent_z)
reader.SetDataSpacing(pixel_size_xy, pixel_size_xy, section_thickness)
reader.SetDataScalarTypeToUnsignedChar()
reader.SetNumberOfScalarComponents(1)
My code to generate the hdf5 file:
with h5py.File(output_file,'w') as h5f:
ds = h5f.create_dataset('images',dtype=np.uint8,shape=(len(image_files),image_height,image_width))
# Iterate over the PNG image files and add them to the dataset
for i, filename in enumerate(tqdm(image_files)):
filepath = os.path.join(image_dir, filename)
with Image.open(filepath) as img:
# Convert grayscale image to numpy array and save it to the HDF5 dataset
img = img.convert("L")
img = np.asarray(img)
ds[i,:,:] = img
Any help would be appreciated. Thanks!