1
  • 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)
Bilal
  • 3,191
  • 4
  • 21
  • 49
  • Use `pydicom.dcmwrite(filename, dico)`. Besides, `"None"` is not a valid date - did you mean `None`? – MrBean Bremen Apr 25 '20 at 12:42
  • @MrBeanBremen I changed birthdate to `None`, and used `pydicom.dcmwrite(filename, dico)`, and got the following error : `AttributeError: 'dict' object has no attribute 'is_little_endian'` – Bilal Apr 25 '20 at 12:53
  • 1
    Ah yes, you have to set `is_little_endian` and `is_implicite` in the dataset. Should be in the documentation somewhere, I can look this up later today (no time right now). – MrBean Bremen Apr 25 '20 at 13:15
  • @MrBeanBremen Thanks in advance. – Bilal Apr 25 '20 at 14:19

1 Answers1

6

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.

MrBean Bremen
  • 14,916
  • 3
  • 26
  • 46
  • @mrbean-bermen `dico.save_as(os.path.join(path,'Anonymous.dcm'))` `AttributeError: 'dict' object has no attribute 'save_as'`, after reading the dicom image it turns to be a dictionary, how can I convert it to FileDataset object again to be able to do `save_as` – Bilal Apr 25 '20 at 15:02
  • This is strange - that should not happen. Which pydicom version do you use? And are you saying that `type(dico)` after assigning it says `dict`? – MrBean Bremen Apr 25 '20 at 15:05
  • @MrBean-Bermen it worked now, thanks, I had copied the data from file to file so they had got converted, I will fix it, thanks again. – Bilal Apr 25 '20 at 15:09
  • 1
    Glad I could help :) – MrBean Bremen Apr 25 '20 at 15:10