# reading and writng a dicom image

**URL:** https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992
**Category:** Beginner Questions
**Tags:** python, dicom, simpleitk
**Created:** [March 29, 2021, 11:41am UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992 "2021-03-29T11:41:12Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![sarbabi](https://discourse.itk.org/user_avatar/discourse.itk.org/sarbabi/32/1995_2.png) [@sarbabi](https://discourse.itk.org/u/sarbabi)
#### Post date: [March 29, 2021, 11:41am UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992/1 "2021-03-29T11:41:12Z")

</div>

Hi,

In the following simple piece of code I’m trying to read a DICOM image as sitkImage and its tags as a python dictionary. then I call another function to write that image in another location with the same tags reading from the dictionary.

yet the output dicom folder is shown as 180 separate slices in the output folder (when I open the folder with a DICOM viewer) and not as a single multi slice image.

here’s the code:

```auto
    import SimpleITK as sitk
    import sys, os, json, time

    dicom_directory = "D:\\0-image_orig"
    output_directory = "D:\\1-image_copy"
    #reads DICOM, returns dicom tags and image3D
    def read_dicom_tags(dicom_dir):
        series_IDs = sitk.ImageSeriesReader.GetGDCMSeriesIDs(dicom_dir)
        series_file_names = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(
            dicom_dir, series_IDs[0])

        series_reader = sitk.ImageSeriesReader()
        series_reader.SetFileNames(series_file_names)

        series_reader.MetaDataDictionaryArrayUpdateOn()
        series_reader.LoadPrivateTagsOn()
        image = series_reader.Execute()

        tags_dict = {}
        for j, key in enumerate(series_reader.GetMetaDataKeys(1)):
            if(key != "ITK_non_uniform_sampling_deviation"):
                tags_dict[key] = series_reader.GetMetaData(0, key)

        dicom_tags_file_address = os.path.join(dicom_dir, '../', dicom_dir.split('\\')[-1]+'_tags.json')
        with open(os.path.join(dicom_dir, '../', dicom_dir.split('\\')[-1]+'_tags.json'), 'w') as f:
            json.dump(tags_dict, f)
        return dicom_tags_file_address, tags_dict, image

    #writes image3D with the dicom tags
    def write_dicom_image_with_tags(image, dicom_tags, output_dir):
        for i in range(image.GetDepth()):
            writer = sitk.ImageFileWriter()
            writer.KeepOriginalImageUIDOn()
            image_slice = image[:, :, i]
            # Tags shared by the series.
            for j, key in enumerate(dicom_tags):
                image_slice.SetMetaData(key, dicom_tags[key])
            # Write to the output directory and add the extension dcm, to force writing in DICOM format.
            image_slice.SetMetaData("0008|0012", time.strftime("%Y%m%d")) # Instance Creation Date
            image_slice.SetMetaData("0008|0013", time.strftime("%H%M%S")) # Instance Creation Time
            image_slice.SetMetaData("0020|0032", '\\'.join(map(str,image3D.TransformIndexToPhysicalPoint((0,0,i))))) # Image Position (Patient)
            image_slice.SetMetaData("0020,0013", str(i)) # Instance Number
            writer.SetFileName(os.path.join(output_dir, str(i) + '.dcm'))
            writer.Execute(image_slice)

    tagsFile, tagsDict, image3D = read_dicom_tags(dicom_directory)
    write_dicom_image_with_tags(image3D, tagsDict, output_directory)

```

any thoughts on what am I missing in here?  
Thanks in advance

---

<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: [March 29, 2021, 1:52pm UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992/2 "2021-03-29T13:52:04Z")

</div>

Hello @sarbabi,

In your `write_dicom_image_with_tags` function the study instance UID (0020|000D) and series instance UID (0020|000E) are set independently for each of the written images. I don’t think they exist in your `dicom_tags` parameter.

If you want to add this processed volume into the existing study, just take the study UID from the original data and create a new series instance UID (see [example](https://simpleitk.readthedocs.io/en/master/link_DicomSeriesReadModifyWrite_docs.html)). Otherwise create study and series UIDs and pass those as part of the `dicom_tags` parameter.

---

<div class="post-metadata">

### Author: ![sarbabi](https://discourse.itk.org/user_avatar/discourse.itk.org/sarbabi/32/1995_2.png) [@sarbabi](https://discourse.itk.org/u/sarbabi)
#### Post date: [March 29, 2021, 2:15pm UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992/3 "2021-03-29T14:15:41Z")

</div>

Hi @zivy ,

Thank you, in the example you mentioned I’m just changing the directory address for original and output image and get this error:

```
Traceback (most recent call last):
  File "example.py", line 111, in <module>
    writer.Execute(image_slice)
  File "Python39\lib\site-packages\simpleitk-2.0.0rc2.dev908+g8244e-py3.9-win-amd64.egg\SimpleITK\SimpleITK.py", line 8172, in Execute
    return _SimpleITK.ImageFileWriter_Execute(self, *args)
RuntimeError: Exception thrown in SimpleITK ImageFileWriter_Execute: \SimpleElastix\ITK\Modules\IO\GDCM\src\itkGDCMImageIO.cxx:1154:
itk::ERROR: GDCMImageIO(0000021536F5E630): A Floating point buffer was passed but the stored pixel type was not specified.This is currently not supported

```

then I do a casting like:

```
castFilter = sitk.CastImageFilter()
castFilter.SetOutputPixelType(sitk.sitkInt16)

# Convert floating type image (imgSmooth) to int type (imgFiltered)
filtered_image2 = castFilter.Execute(filtered_image)

```

and this time it writes the image but the slices are still separated in DICOM viewer and not a single multi-slice image.

---

<div class="post-metadata">

### Author: ![sarbabi](https://discourse.itk.org/user_avatar/discourse.itk.org/sarbabi/32/1995_2.png) [@sarbabi](https://discourse.itk.org/u/sarbabi)
#### Post date: [March 29, 2021, 2:42pm UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992/4 "2021-03-29T14:42:14Z")

</div>

So I saved this [example](https://simpleitk.readthedocs.io/en/master/link_DicomSeriesReadModifyWrite_docs.html) in example.py, then ran this in command line:

$ python example.py “source” “destination”

and I get the same error:

```auto
Traceback (most recent call last):
  File "example.py", line 111, in <module>
    writer.Execute(image_slice)
  File "Python39\lib\site-packages\simpleitk-2.0.0rc2.dev908+g8244e-py3.9-win-amd64.egg\SimpleITK\SimpleITK.py", line 8172, in Execute
    return _SimpleITK.ImageFileWriter_Execute(self, *args)
RuntimeError: Exception thrown in SimpleITK ImageFileWriter_Execute: \SimpleElastix\ITK\Modules\IO\GDCM\src\itkGDCMImageIO.cxx:1154:
itk::ERROR: GDCMImageIO(0000021536F5E630): A Floating point buffer was passed but the stored pixel type was not specified.This is currently not supported

```

Is it like the example lacks something or am I missing something?

---

<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: [March 29, 2021, 6:02pm UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992/5 "2021-03-29T18:02:33Z")

</div>

Hello @sarbabi,

From the error message it appears you aren’t actually working directly with SimpleITK. You appear to be working with SimpleElastix that took SimpleITK and added components to it. Also appears to be referring to SimpleITK release candidate 2 for the SimpleITK release 2.0.0.

I would recommend installing SimpleITK directly and working with that (the example does work with the official SimpleITK releases).

---

<div class="post-metadata">

### Author: ![sarbabi](https://discourse.itk.org/user_avatar/discourse.itk.org/sarbabi/32/1995_2.png) [@sarbabi](https://discourse.itk.org/u/sarbabi)
#### Post date: [March 29, 2021, 8:34pm UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992/6 "2021-03-29T20:34:41Z")

</div>

Thank you @zivy,  
actually I’m trying to save the result of a SimpleElastix registration task as a DICOM image.  
So knowing what you just mentioned, how would suggest to do that?

---

<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: [March 29, 2021, 11:23pm UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992/7 "2021-03-29T23:23:00Z")

</div>

Hello @sarbabi,

You should post the issue on the SimpleElastix [GitHub issues](https://github.com/SuperElastix/SimpleElastix/issues) as it should work the way the example I pointed you to works. In the official SimpleITK releases this code is just fine.

A hack workaround:

1. Create another virtual environment with the official SimpleITK (not SimpleElastix calling itself SimpleITK).
2. Run the registration using your SimpleElastix environment and write the result of the registration transform using an ITK supported format.
3. Switch to the SimpleITK virtual environment, read the transform, read the images, resample and write the output as a DICOM series.

---

<div class="post-metadata">

### Author: ![etauger](https://discourse.itk.org/user_avatar/discourse.itk.org/etauger/32/4546_2.png) [@etauger](https://discourse.itk.org/u/etauger)
#### Post date: [June 26, 2025, 9:59pm UTC](https://discourse.itk.org/t/reading-and-writng-a-dicom-image/3992/8 "2025-06-26T21:59:06Z")

</div>

Hello @sarbabi,

A little late to the party I think, but for posterity and others ending up here as I did; there should have been a pipe (`|`) in the tag where you were setting the instance number instead of a comma (`,`):

```python
image_slice.SetMetaData("0020|0013", str(i)) # Instance Number

```

This way the instance number would have been correctly set and your slices should have appeared as a single multi-slice image as you wanted.

But even better than this, this issue is resolved altogether since tags formatted such as yours (with a comma) are now accepted (see this [PR](https://github.com/InsightSoftwareConsortium/ITK/pull/4926)).
