Errors reading DICOM files

Hello,
I am new to python and ITK.
I want to read a series of Dicom images. For this, I copied the below code snippet from a previous query.

import SimpleITK as sitk

# A file name that belongs to the series we want to read
file_name = '1.dcm'
data_directory = 'CIRS057A_MR_CT_DICOM'

# Read the file's meta-information without reading bulk pixel data
file_reader = sitk.ImageFileReader()
file_reader.SetFileName(file_name)
file_reader.ReadImageInformation()

# Get the sorted file names, opens all files in the directory and reads the meta-information
# without reading the bulk pixel data
series_ID = file_reader.GetMetaData('0020|000e')
sorted_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(data_directory, series_ID)

# Read the bulk pixel data
img = sitk.ReadImage(sorted_file_names)

I am getting the following error

RuntimeError: Exception thrown in SimpleITK ImageFileReader_ReadImageInformation: C:\Miniconda\envs\bld\conda-bld\work\Code\IO\src\sitkImageReaderBase.cxx:97:
sitk::ERROR: The file "1.dcm" does not exist.

Please explain to me what is happening and how to solve this.

Hello @debapriya,

As the error says, a file named 1.dcm cannot be found in the directory in which the code is run. Likely this is because the file is in the data_dirctory and this line:

file_reader.SetFileName(file_name)

should be:

file_reader.SetFileName(os.path.join(data_directory,file_name))

For future reference, when posting code on discourse, please use backticks to format it, as described here. This makes it much more readable and easier to understand.

1 Like

It worked !! Thank you !

For future reference, when posting code on discourse, please use backticks to format it, as [described here](https://meta.discourse.org/t/how-to-post-code-or-preformatted-text/188910). This makes it much more readable and easier to understand.

Sure !