Skip to content

A simple Python script to convert 3D NIfTI files (.nii or .nii.gz) into MP4 videos for axial, coronal, and sagittal views.

License

Notifications You must be signed in to change notification settings

RivMt/nifti2mp4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NIfTI to MP4 Converter

A simple Python script to convert 3D NIfTI files (.nii or .nii.gz) into MP4 videos for axial, coronal, and sagittal views.

Features

  • Converts a single NIfTI file into three separate MP4 videos (axial, coronal, sagittal).
  • Corrects for anisotropic voxel sizes to produce videos with the correct aspect ratio.
  • Allows customization of video FPS and intensity normalization range.
  • Command-line interface for easy use.

Getting Started

Prerequisites

  • Python 3.8+
  • git

Installation

  1. Clone the repository:

    git clone https://github.com/RivMt/nifti2mp4.git
    cd nifti2mp4
  2. Create and activate a virtual environment:

    # Create the environment
    python -m venv venv
    # Activate the environment
    # On Linux/macOS:
    source venv/bin/activate
    
    # On Windows (Command Prompt):
    venv\Scripts\activate.bat
    
    # On Windows (PowerShell):
    .\venv\Scripts\Activate.ps1
  3. Install the required packages: A requirements.txt file is provided for managing dependencies.

    pip install -r requirements.txt

Usage

Run the script from your terminal with the virtual environment activated.

python main.py <path/to/input.nii.gz> <path/to/output_prefix>

Arguments:

  • input_file: Path to the input NIfTI file (.nii or .nii.gz).
  • output_prefix: The prefix for the output MP4 files. This can include a directory path.

Example:

python main.py data/brain.nii.gz results/my_scan

This command will generate three files in the results/ directory:

  • results/my_scan-axial.mp4
  • results/my_scan-coronal.mp4
  • results/my_scan-sagittal.mp4

Note: The script will automatically create the output directory (e.g., results/) if it does not exist.

Command-line Options

You can customize the output with the following options:

  • --fps <int>: Sets the frames per second for the output videos. (Default: 30)
  • --q_min <float>: Sets the lower percentile for intensity normalization. Values below this will be clipped. (Default: 0.01)
  • --q_max <float>: Sets the upper percentile for intensity normalization. Values above this will be clipped. (Default: 0.99)

Example with options:

python main.py data/brain.nii.gz results/my_scan --fps 15 --q_max 0.98

Contributing

Contributions are welcome! If you find a bug or have a suggestion for a new feature, please feel free to open an issue or submit a pull request.

About

A simple Python script to convert 3D NIfTI files (.nii or .nii.gz) into MP4 videos for axial, coronal, and sagittal views.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages