A simple Python script to convert 3D NIfTI files (.nii or .nii.gz) into MP4 videos for axial, coronal, and sagittal views.
- 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.
- Python 3.8+
git
-
Clone the repository:
git clone https://github.com/RivMt/nifti2mp4.git cd nifti2mp4 -
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
-
Install the required packages: A
requirements.txtfile is provided for managing dependencies.pip install -r requirements.txt
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 (.niior.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_scanThis command will generate three files in the results/ directory:
results/my_scan-axial.mp4results/my_scan-coronal.mp4results/my_scan-sagittal.mp4
Note: The script will automatically create the output directory (e.g.,
results/) if it does not exist.
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.98Contributions 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.