Loading DICOM files in Python

1,177 views
Skip to first unread message

dejana....@gmail.com

unread,
Jan 26, 2017, 12:52:03 PM1/26/17
to pydicom
Hello,

I am new with pydicom, so I have some problems with it.
For Python 3.4, pydicom 0.99, when I try loading DICOM files, I get this error:

import dicom
import os
import numpy

PathDicom = "C:\Python34\Baza"
lstFilesDCM = [] # create an empty list
for dirName, subdirList, fileList in os.walk(PathDicom):
for filename in fileList:
if ".dcm" in filename.lower(): # check whether the file's DICOM
lstFilesDCM.append(os.path.join(dirName,filename))

# Get ref file
RefDs = dicom.read_file(lstFilesDCM[0])

# Load dimensions based on the number of rows, columns, and slices (along the Z axis)
ConstPixelDims = (int(RefDs.Rows), int(RefDs.Columns), len(lstFilesDCM))

# Load spacing values (in mm)
ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness)) ERROR: C:\Python34\python.exe C:/Users/041213/PycharmProjects/untitled1/Dejana.py Traceback (most recent call last): File "C:/Users/041213/PycharmProjects/untitled1/Dejana.py", line 13, in <module> RefDs = dicom.read_file(lstFilesDCM[0]) IndexError: list index out of range If someone could help me with this, I would really appreciate.
Or if someone could help me how do I load DICOM files in general?

Sorry for a long post,

Greetings,
Dejana

Jonathan Suever

unread,
Jan 26, 2017, 3:56:13 PM1/26/17
to pyd...@googlegroups.com
Dejana,

The issue does not appear to be a problem with pydicom. Instead, it seems that lstFilesDCM is an empty list (i.e. your script didn't find any files ending in ".dcm" in the specified folder) therefore when you try to access the first element using lstFilesDCM[0] it fails resulting in the IndexError ("list index out of range").

-Jonathan

--
You received this message because you are subscribed to the Google Groups "pydicom" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydicom+unsubscribe@googlegroups.com.
To post to this group, send email to pyd...@googlegroups.com.
Visit this group at https://groups.google.com/group/pydicom.
For more options, visit https://groups.google.com/d/optout.

dejana....@gmail.com

unread,
Jan 26, 2017, 4:41:40 PM1/26/17
to pydicom

Yes, I understand that, but the problem is that is not an empty file, it consists of 20 dcm files.
Even when I use the most simple code, it won't work.
For example:


import dicom
filePath="C:\Python34\Lib\site-packages\dicom\testfiles"
ds=dicom.read_file(filePath[0])
Error is: 
C:\Python34\python.exe C:/Users/041213/PycharmProjects/D/Deki.py Traceback (most recent call last):
 File "C:/Users/041213/PycharmProjects/D/Deki.py", line 4, in
    ds=dicom.read_file(filePath[0]) 
File "C:\Python34\lib\site-packages\dicom\filereader.py", line 589, in read_file 
    fp = open(fp, 'rb') FileNotFoundError: [Errno 2]
 No such file or directory: 'C'

Aditya Panchal

unread,
Jan 26, 2017, 4:53:07 PM1/26/17
to pyd...@googlegroups.com
You may want to escape your filePath as a raw string due to the single backslashes on Windows masquerading as escape characters:

filePath=r"C:\Python34\Lib\site-packages\dicom\testfiles"
So you need to insert an 'r' before the string.

Otherwise you'll need to double backslash your file path.

Adit

--

Eli Stevens (Gmail)

unread,
Jan 26, 2017, 5:45:27 PM1/26/17
to pyd...@googlegroups.com
Also, the code you gave is incorrect:

filePath="C:\Python34\Lib\site-packages\dicom\testfiles"
ds=dicom.read_file(filePath[0])

Here, filePath[0] is just "C" since it's a string, not a list of strings.

I recommend starting to print out the repr of values you are passing
into APIs to make sure they're what you expect. Gives you quick
feedback about stuff. That would have caught the backslash issue too.

Cheers,
Eli
>> email to pydicom+u...@googlegroups.com.
>> To post to this group, send email to pyd...@googlegroups.com.
>> Visit this group at https://groups.google.com/group/pydicom.
>> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "pydicom" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pydicom+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages