is it possible to write/save to disk all the meta-data of a DICOM image without explicitly stating which tags to copy?

I am resampling a DICOM CT image and after resampling I would like to save it to disk as new image.

I found this example which unfortunately specify all the tags to copy before writing a DICOM image slice to disk.

I want to avoid that. Ideally I would have something as simple as pydicom.dataset.save_as(filepath).

Is that possible without writing down all the tags I want to save to disk?

Thank you

Hi @ral_san,
This is relatively straightforward (famous last words). First, be aware that it cannot be as easy as save_data because you have created a new series in this “study”, so you don’t want the new images to have exactly the same meta-data information as the original image.

You can modify the example as follows:

  1. Copy all the meta-data dictionary tags that are shared by the series from the series_reader (likely all the tags are shared so just iterate over the keys and get the values) into a local dictionary series_tag_values.
  2. In your local dictionary set the value for the tags you need to modify, e.g. series_tag_values["0008|0031"]=modification_time
  3. Iterate over the slices and set the tag-value as done in the example.

I think this does what you need?

1 Like

There are many DICOM tags that are related to spatial information, e.g. spacing. After resampling, for example, they will be invalid if they are copied as-is.

I understand that meta-data needs to be updated in this case, where resampling if performed.
However, what I want to avoid is iterating over all of the keys. Basically, I was wondering if there is only one line of code that copies all the meta-data tags in one go. So then I only have to modify the tags related to resampling such as spacing, etc.

How about something like this:

itk::GDCMImageIO::Pointer dicomIO = itk::GDCMImageIO::New();
itk::MetaDataDictionary metadata = dicomIO->GetMetaDataDictionary();
// modify metadata if needed

How is two lines in SimpleITK + printing:

for key in img.GetMetaDataKeys(): 
   print("{0}: {1}".format(key, img.GetMetaData(key))) 
   img2.SetMetaData(key, img.GetMetaData(key))

Generally blindly coping tags is discouraged.

Hi @ral_san,

Generally copying a meta-data dictionary as is, is dangerous as you will have two images with the same study-series-image ids. This is never a good idea. If all files sit in the same directory no DICOM reader will be able to separate the two.

Bottom line, be careful.

1 Like