0
votes

I want to read through images in different folders. I wrote the following code

     for Case_id in range(1,6):

     path ='/Users/XXXXXX/Desktop/pyradiomics/Converted/Case{}/'.format(Case_id)
     print(path)
     for files in os.listdir(path):
       if files.endswith("Image.nii"):
          print(files)
          image=sitk.ReadImage (files)
       if files.endswith("label.nii"):
          print(files)
          mask=sitk.ReadImage (files)

When I run this I get an error message:

RuntimeError: Exception thrown in SimpleITK ReadImage: /scratch/dashboard/SimpleITK-OSX10.6-x86_64-pkg/SimpleITK/Code/IO/src/sitkImageReaderBase.cxx:89:

sitk::ERROR: The file "xxxx_image.nii" does not exist.

If I just run the print command I can see all the files along with path in the specified folder. Would appreciate the help.

2

2 Answers

1
votes

@dave-chen is correct. You need to join the path to get the full path. Try:

 for Case_id in range(1,6):

 path ='/Users/XXXXXX/Desktop/pyradiomics/Converted/Case{}/'.format(Case_id)
 print(path)
 for files in os.listdir(path):
   if files.endswith("Image.nii"):
      print(files)
      image=sitk.ReadImage(os.path.join(path, files))
   if files.endswith("label.nii"):
      print(files)
      mask=sitk.ReadImage(os.path.join(path, files))
0
votes

I'm guessing you need to pass the full path name to ReadImage. 'files' is only the name of the file. If you're not running the script in that 'path' directory, ReadImage won't fine the files, so it's going to look in the current working directory.