0

I am new to coding. I basically have a bunch of files in "nifti" format, I wanted to simply load them, apply a thresholding function to them and then save them. I was able to write the few lines of code to do it to one file (it worked), but I have many so I created another python file and tried to make a for loop. I think it does everything fine but the last step for saving my files just keeps overwriting so in the end I only get one output file.

import numpy as np
import nibabel as nb
import glob 
import os


path= 'subjects'
all_files=glob.glob(path + '/*.nii')

for filename in all_files:
    image=nb.load(filename)
    data=image.get_fdata()
    data [data<0.1]=0
    new_image=nb.Nifti1Image(data, affine=image.affine, header=image.header)
   
    nb.save(new_image,filename+1)
era
  • 1
  • 1
  • I am wondering how the code can work in the first place. The name you are providing for saving is `filename+1`, where filename should be a string and 1 is an integer. You should get a TypeError. Are you sure this code is running without raising an error? – Flow Nov 07 '22 at 07:32
  • Also, since each filename also consists of the file extension you need to make sure to change the filename while keeping the extension intact. – Flow Nov 07 '22 at 07:34

0 Answers0