Untitled

 avatar
unknown
plain_text
a year ago
1.6 kB
8
Indexable
import h5py
import os

def find_hdf5_files(root_folder):
    """Recursively find all HDF5 files in the given root folder."""
    hdf5_files = []
    for subdir, _, files in os.walk(root_folder):
        for file in files:
            if file.endswith('.hdf5'):
                hdf5_files.append(os.path.join(subdir, file))
    return hdf5_files

def extract_first_image(hdf5_path):
    """Extract the first image from the 'entry/data/data' dataset of an HDF5 file."""
    with h5py.File(hdf5_path, 'r') as file:
        data = file['entry/data/data'][0]  # Assuming the first image can be indexed like this
    return data

def create_output_file(output_path, images):
    """Create an output HDF5 file and append all images to the 'entry/data/data' dataset."""
    with h5py.File(output_path, 'w') as file:
        # Determine the shape and dtype for the dataset based on the first image
        dataset = file.create_dataset("entry/data/data", shape=(len(images), *images[0].shape), dtype=images[0].dtype)
        for i, image in enumerate(images):
            dataset[i] = image

def process_hdf5_files(root_folder):
    """Process all HDF5 files in the given root folder and create an output file with all first images."""
    hdf5_files = find_hdf5_files(root_folder)
    first_images = [extract_first_image(file) for file in hdf5_files]
    output_path = os.path.join(root_folder, "output.hdf5")
    create_output_file(output_path, first_images)

# Example usage
root_folder = 'path/to/your/folder'
process_hdf5_files(root_folder)

Editor is loading...
Leave a Comment