Can I automate just importing files, changing their type, and then saving them?

I am trying to write a Python script that imports .msg files into 3D Slicer and saves them as nii files. I’ve never automated anything and I am having a bit of a hard time understanding how to write scripts and run them. Thanks.

If you don’t need to do anything interactive with the files, then you probably don’t need to load them into Slicer at all. You might take a look at the slicerio python package and see if you can use that to just load and save the data formats you need.

1 Like

Thanks so much!!! I will check it out!

1 Like

I ended up using freesurfer’s mri_convert command and wrote a BASH script to loop through all the files in a directory and convert them. Here’s the code:

#!/bin/bash 

#ask user for filepath and change directory to filepath 
read -p "Enter filepath: " fp  
cd $fp 

#loop through files 
for FILE in * 
do 
        #seperate file extentsion and name 
        filepath="${fp}${FILE}" 
        filename_with_ext=$(basename $filepath) 
        filename="${filename_with_ext%.*}" 
        extension="${filename_with_ext##*.}" 

        #concatinate filename with new extension         
        ni_file="${filename}.nii" 

        #convert to nii using now seperated file name concaqtinated with .nii 
        mri_convert --in_type mgz --out_type nii ${FILE} ${ni_file} 
done 

 #switch back to home directory 
cd ~ 
2 Likes