File size: 1,661 Bytes
b903156 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #!/usr/bin/python
# Create bitmaps to check defacing of all NIFTI images
# python bids_deface_bitmaps.py
from pathlib import Path
import os
import os.path
import glob
import sys
import shutil
import fnmatch
def remove_after_final_underscore(input_string):
# Find the last underscore index
last_underscore_index = input_string.rfind('_')
# If an underscore is found, remove characters after it
if last_underscore_index != -1:
result_string = input_string[:last_underscore_index]
return result_string
else:
# If no underscore is found, return the original string
return input_string
if __name__ == "__main__":
inputRoot = './'
outRoot = './bmps/'
exe = '/Applications/MRIcroGL.app/Contents/MacOS/MRIcroGL'
if not shutil.which(exe):
sys.exit("Unable to find exe: "+exe)
nifti_files = [f for f in os.listdir(inputRoot) if fnmatch.fnmatch(f, 'wsub-M4209*.nii.gz')]
for subname in list(nifti_files):
sub = Path(subname).stem
les = os.path.join(inputRoot, subname)
#wbsub-M2185_ses-388_T1w.nii.gz
T1 = remove_after_final_underscore(sub)
bmp = os.path.join(outRoot, T1 + ".png")
T1 = T1[:1] + 'b' + T1[1:] + '_T1w.nii.gz'
T1 = os.path.join(inputRoot, T1);
if not os.path.isfile(T1):
print('oops: '+ T1);
continue
#print(T1) gl.overlayload
cmd = exe + ' -s \'import gl\ngl.loadimage("' + T1 + '")\ngl.drawload("' + les + '")\ngl.mosaic("A 0.5 S 0.25 C 0.5 S R -0")\ngl.colorbarposition(0)\ngl.savebmp("' + bmp + '")\ngl.quit()\'\n'
os.system(cmd)
#sys.exit()
|