1

I am processing a set of DICOM files, some of which have image information and some of which don't. If a file has image information, the following code works fine.

file_reader = sitk.ImageFileReader()
file_reader.SetFileName(fileName)
file_reader.ReadImageInformation()

However, if the file does not have image information, I get the following error.

Traceback (most recent call last):

  File "<ipython-input-61-d187aed107ed>", line 5, in <module>
    file_reader.ReadImageInformation()

  File "/home/peter/anaconda3/lib/python3.7/site-packages/SimpleITK/SimpleITK.py", line 8673, in ReadImageInformation
    return _SimpleITK.ImageFileReader_ReadImageInformation(self)

RuntimeError: Exception thrown in SimpleITK ImageFileReader_ReadImageInformation: /tmp/SimpleITK/Code/IO/src/sitkImageReaderBase.cxx:107:
sitk::ERROR: Unable to determine ImageIO reader for "/path/115.dcm"

If the DICOM file has no information, I would like to just ignore the file rather than calling ReadImageInformation(). Is there a way to check whether ReadImageInformation() will work before it is called? I tried the following and they are no different between files where ReadImageInformation() and files where it does not.

file_reader.GetImageIO()
file_reader.GetMetaDataKeys() # Crashes
file_reader.GetDimension()
OtagoHarbour
  • 3,969
  • 6
  • 43
  • 81

1 Answers1

1

I would just put an exception handler around it to catch the error. So it'd look something like this:

file_reader = sitk.ImageFileReader()
file_reader.SetFileName(fileName)
try:
    file_reader.ReadImageInformation()
except:
    print(fileName, "has no image information")
Dave Chen
  • 1,905
  • 1
  • 12
  • 18