Ellie
Ellie

Reputation: 303

AttributeError: 'FileDataset' object has no attribute 'ImagePosition'

for person in range(0, len(dirs1)):
    for root, dirs, files in os.walk(os.path.join(path, dirs1[person])):
        dcmfiles = [_ for _ in files if _.endswith('.dcm')]
        for dcmfile in dcmfiles:
            dcm_image = pydicom.read_file(os.path.join(root, dcmfile))
            img = dcm_image.pixel_array
            img2 = dcm_image.ImagePosition   # Error in this line

for:

(0020, 0032) Image Position (Patient) DS: ['-166.000000', '-171.699997', '-207.500000']

My problem is that I want the "Image Position (Patient)" structure as an array or one element of it (Like '-207.500000'). And when I run the code, this error occurs: the line of img2 = dcm_image.ImagePosition gives AttributeError: 'FileDataset' object has no attribute 'ImagePosition'

Upvotes: 0

Views: 6603

Answers (2)

Javaid Iqbal
Javaid Iqbal

Reputation: 157

It has resolved to use the builtin function

# Display each DICOM slice
for i, dcm in enumerate(dicom_files):

img = dcm.pixel_array
img2 = dcm.ImagePositionPatient

# height, width, depth = dcm.shape
# print(f"dcm {i + 1} - Size: Height: {height}, Width: {width}, 
Depth: {depth}")

plt.figure(figsize=(6, 6))
plt.imshow(dcm.pixel_array, cmap='gray')
plt.title(f"Slice {i + 1}")
plt.axis('off')
plt.show()

Upvotes: 0

g_uint
g_uint

Reputation: 2041

The Image Position (Patient) attribute is, as Karl suggested in his comment, accessible with

dmc_image.ImagePositionPatient

If the error stil occurs with this attribute it means what the error indicates: your object does not have this attribute. Dicom states that the keyword for the (0020,0032) tag is ImagePositionPatient, and ImagePosition is actuallly a retired tag (0020, 0030). See DICOM Data Dictionary, page 59. So the fact that your object does not have it is probably a good thing.

Upvotes: 1

Related Questions