Search code examples
pydicom

How to save Dicom Image with dicom format after anonymization?


  • I read a dicom image with all metadate, and anonymized some fields, I want to write it again after that, but I get the error: if None in (dataset.is_little_endian, dataset.is_implicit_VR): AttributeError: 'dict' object has no attribute 'is_little_endian'
  • I read the documentation of pydicom, but didn't understand how to do it!
  • How can I write it again in one working instruction ?
  • Edit: Birthdate changed to None instead of 'None', and dcmwrite insted of save_as.
import pydicom
from pydicom.misc import is_dicom

fp ='1.dcm'
dico = pydicom.filereader.dcmread(fp)
if(is_dicom(dico)):
        dico['PatientID']= 'None'
        dico['PatientBirthDate'] = None
        dico['is_little_endian'] = True
        dico['is_implicit_VR'] = True
        path = '/dataset'
        # dico.save_as(os.path.join(path,'Anonymous.dcm'))
        pydicom.dcmwrite(os.path.join(path,'Anonymous.dcm'), dico)

Solution

  • Ok, using save_as should actually work, if you use it as in your first attempt. Here is the code that should work:

    import pydicom
    from pydicom.misc import is_dicom
    
    dico = pydicom.filereader.dcmread('1.dcm')
    dico.PatientID = 'None'
    dico.PatientBirthDate = None
    path = '/dataset'
    dico.save_as(os.path.join(path,'Anonymous.dcm'))
    # alternatively:
    # dcmwrite(os.path.join(path,'Anonymous.dcm', dico)
    

    Note that I have changed dico['PatientID'] to dico.PatientID. This is not only a convenient shortcut, but also changes the semantics: if you assign to dico['PatientID'], you have to assign a DataElement:

    dico['PatientID'] = DataElement(0x00100020, 'PN', b'None')
    

    whereas if you use the keyword, you can directly assign the value (which is converted to a DataElement internally).

    I agree that the documentation is somewhat lacking in that aspect - I think it would make sense to add a simple example for reading a DICOM file, modifying it, and writing it back. But if you check the basic dataset documentation, you should find most of the needed information.

    A note regarding the mentioned properties is_little_endian and is_implicit_VR: these are only needed if you write a new dataset that does not have a transfer syntax set. Here is an example for that case. If the dataset is read from a valid DICOM file, it has these properties already set.