|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Convert a directory of image files (TIFF, PNG, JPG, etc.) into a single 3D HDF5 volume. |
| 4 | +
|
| 5 | +Supports various image formats: |
| 6 | +- TIFF (single or multi-page) |
| 7 | +- PNG |
| 8 | +- JPEG/JPG |
| 9 | +- BMP |
| 10 | +- And any other formats supported by the I/O backend |
| 11 | +
|
| 12 | +Usage: |
| 13 | + python scripts/images_to_h5.py <input_pattern> <output_file.h5> [dataset_key] |
| 14 | +
|
| 15 | +Examples: |
| 16 | + # TIFF files (default dataset key: 'main') |
| 17 | + python scripts/images_to_h5.py "datasets/bouton-lv/LV_EM/*.tiff" datasets/bouton-lv/LV_EM.h5 |
| 18 | +
|
| 19 | + # PNG files (default dataset key: 'main') |
| 20 | + python scripts/images_to_h5.py "/path/to/images/*.png" output.h5 |
| 21 | +
|
| 22 | + # Custom dataset key |
| 23 | + python scripts/images_to_h5.py "/path/to/labels/*.png" labels.h5 labels |
| 24 | +
|
| 25 | +Note: Use quotes around the input pattern to prevent shell expansion. |
| 26 | +""" |
| 27 | + |
| 28 | +import sys |
| 29 | +import os |
| 30 | +from pathlib import Path |
| 31 | +from connectomics.data.io import read_volume, write_hdf5 |
| 32 | + |
| 33 | + |
| 34 | +def main(): |
| 35 | + """Main conversion function.""" |
| 36 | + if len(sys.argv) < 3: |
| 37 | + print("Usage: python scripts/images_to_h5.py <input_pattern> <output_file.h5> [dataset_key]") |
| 38 | + print("") |
| 39 | + print("Examples:") |
| 40 | + print(' python scripts/images_to_h5.py "datasets/images/*.tiff" output.h5') |
| 41 | + print(' python scripts/images_to_h5.py "/path/to/images/*.png" output.h5') |
| 42 | + print(' python scripts/images_to_h5.py "/path/to/labels/*.png" labels.h5 labels') |
| 43 | + print("") |
| 44 | + print("Note: Use quotes around the input pattern to prevent shell expansion.") |
| 45 | + sys.exit(1) |
| 46 | + |
| 47 | + input_pattern = sys.argv[1] |
| 48 | + output_file = sys.argv[2] |
| 49 | + dataset_key = sys.argv[3] if len(sys.argv) > 3 else "main" |
| 50 | + |
| 51 | + # Ensure output directory exists |
| 52 | + output_dir = os.path.dirname(output_file) |
| 53 | + if output_dir and not os.path.exists(output_dir): |
| 54 | + print(f"Creating output directory: {output_dir}") |
| 55 | + os.makedirs(output_dir, exist_ok=True) |
| 56 | + |
| 57 | + # Detect file format from pattern |
| 58 | + pattern_lower = input_pattern.lower() |
| 59 | + if any(ext in pattern_lower for ext in ['.tif', '.tiff']): |
| 60 | + format_name = "TIFF" |
| 61 | + elif '.png' in pattern_lower: |
| 62 | + format_name = "PNG" |
| 63 | + elif any(ext in pattern_lower for ext in ['.jpg', '.jpeg']): |
| 64 | + format_name = "JPEG" |
| 65 | + else: |
| 66 | + format_name = "image" |
| 67 | + |
| 68 | + print(f"Reading {format_name} files matching: {input_pattern}") |
| 69 | + print("This may take a while for large volumes...") |
| 70 | + |
| 71 | + # Read all image files as a 3D volume |
| 72 | + try: |
| 73 | + volume = read_volume(input_pattern) |
| 74 | + except Exception as e: |
| 75 | + print(f"Error reading images: {e}") |
| 76 | + print("\nTips:") |
| 77 | + print(" - Check that the file pattern is correct") |
| 78 | + print(" - Ensure all images have the same dimensions") |
| 79 | + print(" - Verify the image files are readable") |
| 80 | + sys.exit(1) |
| 81 | + |
| 82 | + print(f"\n{'='*60}") |
| 83 | + print(f"Volume Information:") |
| 84 | + print(f"{'='*60}") |
| 85 | + print(f" Shape: {volume.shape}") |
| 86 | + print(f" Data type: {volume.dtype}") |
| 87 | + print(f" Size: {volume.nbytes / (1024**3):.2f} GB") |
| 88 | + print(f" Min value: {volume.min()}") |
| 89 | + print(f" Max value: {volume.max()}") |
| 90 | + print(f"{'='*60}") |
| 91 | + |
| 92 | + print(f"\nSaving to: {output_file}") |
| 93 | + print(f"Dataset key: '{dataset_key}'") |
| 94 | + |
| 95 | + # Save as HDF5 |
| 96 | + try: |
| 97 | + write_hdf5(output_file, volume, dataset=dataset_key) |
| 98 | + print(f"\n✓ Successfully saved 3D volume to {output_file}") |
| 99 | + print(f" You can access it with h5py using: f['{dataset_key}']") |
| 100 | + except Exception as e: |
| 101 | + print(f"\n✗ Error writing HDF5 file: {e}") |
| 102 | + sys.exit(1) |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments