Mrconvert at once

Hi, I am the researcher who focusing on neuroimaging such as MRI, PET. I have a question about this program. I want to convert 500 mgz data into nifti files at once using the mrconvert code. How can I do that? Thanks:)

Hi @Corgito,

Assuming you want to convert each .mgz file into its own .nii, and that all the relevant files are in the same folder, you can do that with a simple bash for loop and a bit of parameter substitution (see our command-line tutorial for details):

for f in *.mgz; do mrconvert $f ${f%.*}.nii; done

or if you wanted compressed NIfTI:

for f in *.mgz; do mrconvert $f ${f%.*}.nii.gz; done

If the files you need to convert are not the only .mgz files in the folder, but they all have the same prefix, or some other distinguishing filename, you can use a more selective pattern to match. For example, if all the .mgz files you need to convert have a format like pat-1234-final.mgz, you can match that using e.g.:

for f in pat-*-final.mgz; do mrconvert $f ${f%.*}.nii; done

There’s a lot you can do using bash, and none of it is specific to MRtrix. It’s all standard Unix, you’ll find plenty of information and tutorials online if you need to refine or do something subtly different.


Another option is to use our own for_each script, which has a slightly simpler syntax, and also allows you to run these jobs in parallel (not recommended for this task, mrconvert is already fully multi-threaded, and file conversions are typically dominated by read/write throughput, not compute performance).

for_each *.mgz : mrconvert IN PRE.nii

Personally, I tend to stick with bash for loops since they do the same job, are available on any Unix system, and provide more flexibility when I need it. But for_each is also an option and you can definitely use it.


If on the other hand you wanted to concatenate all 500 .mgz images into a single .nii, that can be done using mrcat (you may need to select a different axis, depending on the data and what you’re trying to achieve):

mrcat *.mgz -axis 3 combined.nii

I hope that helps.
All the best,
Donald

1 Like

Hey, thank you for your help, but I have one more question. When I use bash file. It shows this kind of error message. How can I fix it?
[ERROR] Expected exactly 2 arguments (1 supplied)
mrconvert: [ERROR] Usage: mrconvert input output
mrconvert: [ERROR] Yours: mrconvert .nii

Well, that depends on exactly what you typed… Looking at the error message, I’m guessing you might have forgotten one of the $f in the mrconvert call…?


[EDIT] actually, it looks like the $f variable is empty, which would happen if *.mgz is not matching any files in the current folder. Are you running this from within the folder of interest? If not, you need to cd into the right place before running the command, or edit the pattern to match, for example:

cd data/my_study/data_in_mgz_format/
for f in *.mgz; do mrconvert $f ${f%.*}.nii; done

or

for f in data/my_study/data_in_mgz_format/*.mgz; do mrconvert $f ${f%.*}.nii; done