Search code examples
pythondicompydicom

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'


Solution

  • 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.