Package: nifti2gif
NIfTI to GIFForewordThe following repository is heavily based onGif_your_niftipackage byLukas Snoek. I forked the repository, and I pushed it to the PyPI repository, so that it is now available via thepipcommand. See the PyPI repository of thenifti2gifproject:https://pypi.org/project/nifti2gif/Quick installationIn order to install:pipinstallnifti2gifBasic usage in a notebook or a script:importnifti2gif.coreasnifti2gifnifti2gif.write_gif_normal('./some_nifti_file.nii')See example usage:https://www.kaggle.com/code/mikolajbuchwald/3d-mri-brain-tumor-segmentation-u-netCreate a nice looking gifs from your nifti (.niior.nii.gz) files with a simple command:gif_your_nifti /path/to/data.niiGetting startedDependenciesPackageTested versionNumPy1.14.2NiBabel2.2.1matplotlib2.2.0imageio2.2.0scikit-image0.13.0InstallationNavigate to the main page of the repository and clone it. Then, change directory to the cloned repository and run:pip install -r requirements.txt
python setup.py installDockerBuild a docker container with:docker build -t gif_your_nifti .Run with docker:docker run --rm -v /path/to/nifti/:/data gif_your_nifti:latest /data/image.niiExamplesFor the examples below, I've downloadedICBM 2009c Nonlinear Asymmetricand used the T1 and gray matter template thereof.It is also possible to use your own brain image, as I will show below. The only thing that you need to make sure is, that your brain image has the right orientation. You can reorient your NIfTI image according to the MNI template standard with the FSL command:fslreorient2std my_brain.nii my_brain.nii.Grayscale GIFTo create a simple gray scale gif, type the following command in your commandline:gif_your_nifti /path/to/mni_icbm152_t1_tal_nlin_asym_09c.niiPseudocolor GIFTo create apseudocolorgif, type the following command in your commandline:gif_your_nifti /path/to/Me_2014.nii --mode pseudocolor --cmap plasmaThe colormap can be any colormap from thematplotlib colormaps.Depth GIFTo create a depth gif, type the following command in your commandline:gif_your_nifti /path/to/mni_icbm152_t1_tal_nlin_asym_09c.nii --mode depthThe image shows you in color what the value of the next slice will be. If the color is slightly red or blue it means that the value on the next slide is brighter or darker, respectifely. It therefore encodes a certain kind of depth into the gif.RGB GIFTo create a Red Blue Green (RGB) gif, type the following command in your commandline:gif_your_nifti /path/to/gm.nii /path/to/wm.nii /path/to/csf.nii --mode rgbThis image takes the values from the first NIfTI file as its red colors, second NIfTI file as its green colors and third NIfTI as its blue colors.Resize GIFIt is also possible to change the size of a gif, by changing thesizeparameter in any function above. The following are examples of resizing the images to 50% of it's original size, with:gif_your_nifti /path/to/mni_icbm152_gm_tal_nlin_asym_09c.nii --size 0.5
gif_your_nifti /path/to/mni_icbm152_gm_tal_nlin_asym_09c.nii --size 0.5 --mode pseudocolor --cmap cubehelix
gif_your_nifti /path/to/mni_icbm152_gm_tal_nlin_asym_09c.nii --size 0.5 --mode pseudocolor --cmap inferno
gif_your_nifti /path/to/mni_icbm152_gm_tal_nlin_asym_09c.nii --size 0.5 --mode pseudocolor --cmap viridisChanging the size of a gif also changes the frames per second parameter, so that the overall tempo stays the same. Meaning, if you have a gif of original size with 20 frames per second (fps), changing the size to 50%, will cause the smaller gif to run at 10 fps, so that both take the same amount for a cycle.Use within a python scriptYou can also usegif_your_niftifrom a python script. Seescript examplesfor further information.Uploading to PyPI repositoryBuild the project:pythonsetup.pysdistUpload withtwine(pip install twine):twineuploaddist/*Type your username and password to PyPI.Troubleshooting with uploading to PyPICheck, e.g., for syntax errors in the description file (README):twinecheckdist/*LicenseThis project is licensed underBSD 3-Clause License.