era
era

Reputation: 1

How do I save each iteration as my file format without overwriting the previous iteration?

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)

Upvotes: 0

Views: 98

Answers (0)

Related Questions