Bilal
Bilal

Reputation: 3855

How to save Dicom Image with dicom format after anonymization?

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)

Upvotes: 1

Views: 2900

Answers (1)

MrBean Bremen
MrBean Bremen

Reputation: 16805

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.

Upvotes: 6

Related Questions