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

**URL:** https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125
**Category:** Uncategorized
**Tags:** dicom, simpleitk, python
**Created:** [August 6, 2019, 9:21am UTC](https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125 "2019-08-06T09:21:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ral\_san](https://discourse.itk.org/user_avatar/discourse.itk.org/ral_san/32/236_2.png) [@ral\_san](https://discourse.itk.org/u/ral_san)
#### Post date: [August 6, 2019, 9:21am UTC](https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125/1 "2019-08-06T09:21:40Z")

</div>

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 [https://simpleitk.readthedocs.io/en/master/Examples/DicomSeriesReadModifyWrite/Documentation.html](https://simpleitk.readthedocs.io/en/master/Examples/DicomSeriesReadModifyWrite/Documentation.html) 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

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [August 6, 2019, 1:20pm UTC](https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125/2 "2019-08-06T13:20:59Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [August 9, 2019, 3:42am UTC](https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125/3 "2019-08-09T03:42:35Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ral\_san](https://discourse.itk.org/user_avatar/discourse.itk.org/ral_san/32/236_2.png) [@ral\_san](https://discourse.itk.org/u/ral_san)
#### Post date: [August 14, 2019, 8:38am UTC](https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125/4 "2019-08-14T08:38:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [August 14, 2019, 2:20pm UTC](https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125/5 "2019-08-14T14:20:34Z")

</div>

How about something like this:

```auto
itk::GDCMImageIO::Pointer dicomIO = itk::GDCMImageIO::New();
reader->SetImageIO(dicomIO);
...
reader->Update();
itk::MetaDataDictionary metadata = dicomIO->GetMetaDataDictionary();
...
// modify metadata if needed
image->SetMetaDataDictionary(metadata);
writer->SetInput(image);
writer->Update();

```

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [August 14, 2019, 2:37pm UTC](https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125/6 "2019-08-14T14:37:34Z")

</div>

> [@ral\_san](#):
>
> Basically, I was wondering if there is only one line of code that copies all the meta-data tags in one go

How is two lines in SimpleITK + printing:

```auto
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.

---

<div class="post-metadata">

### Author: ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)
#### Post date: [August 14, 2019, 3:10pm UTC](https://discourse.itk.org/t/is-it-possible-to-write-save-to-disk-all-the-meta-data-of-a-dicom-image-without-explicitly-stating-which-tags-to-copy/2125/7 "2019-08-14T15:10:15Z")

</div>

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.
