# Writing dicom file through using ImageSeriesReader

**URL:** <https://discourse.itk.org/t/writing-dicom-file-through-using-imageseriesreader/2563>\
**Category:** Beginner Questions\
**Tags:** dicom, simpleitk, python\
**Created:** [December 22, 2019, 1:46am UTC](https://discourse.itk.org/t/writing-dicom-file-through-using-imageseriesreader/2563 "2019-12-22T01:46:58Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tatsuaki](https://discourse.itk.org/user_avatar/discourse.itk.org/tatsuaki/32/1153_2.png) [@tatsuaki](https://discourse.itk.org/u/tatsuaki)\
**Post date:** [December 22, 2019, 1:46am UTC](https://discourse.itk.org/t/writing-dicom-file-through-using-imageseriesreader/2563/1 "2019-12-22T01:46:58Z")

</div>

Hi, please help.

I cannot write correctly dicom.  
I did following step,

```auto
# load
L40t1 = "path/to/dcm image dir"
series_IDsT1 = sitk.ImageSeriesReader.GetGDCMSeriesIDs(L40t1)
if not series_IDsT1:
    print("ERROR: given directory does not contain a DICOM series.")
series_file_namesT1 = sitk.ImageSeriesReader.GetGDCMSeriesFileNames(L40t1, series_IDsT1[0])
series_readerT1 = sitk.ImageSeriesReader()
series_readerT1.SetFileNames(series_file_namesT1)
series_readerT1.MetaDataDictionaryArrayUpdateOn()
series_readerT1.LoadPrivateTagsOn()
t1volume = series_readerT1.Execute()

```

then save it,

```auto
writer = sitk.ImageFileWriter()
writer.KeepOriginalImageUIDOn()
print("save images...", t1volume.GetDepth())
for i in range(t1volume.GetDepth()):
    image_slice = t1volume[:,:,i]
    # Tags shared by the series.
    for j,key in enumerate(series_readerT1.GetMetaDataKeys(i)):
        image_slice.SetMetaData(key, series_readerT1.GetMetaData(i,key))
    # Write to the output directory and add the extension dcm, to force writing in DICOM format.
    writer.SetFileName(os.path.join(outdir,str(i)+'.dcm'))
    writer.Execute(image_slice)
print(("~" * 50) + " Done.")

```

then, check pixel spacing of saved image

```auto
# check pixel spacing
print("original",t1volume.GetSpacing())

# data test using 0.dcm
image = sitk.ReadImage(os.path.join(outdir,str(0)+'.dcm')) 
print("saved image",image.GetSpacing())

```

I got result of pixel spacing are,  
original (0.78125, 0.78125, 7.000051021575928)  
saved image (0.78125, 0.78125, 1.0) # Odd…

Why I cannot save SpacingBetweenSlices correctly ??  
Where is wrong/lack of my code ??

But, this behavior occurred by only using ImageSeriesReader. (in my case).  
When I use ReadImage(path2dcm-instance), I can save SpacingBetweenSlices correctly.

tatsuaki

---

<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:** [December 22, 2019, 10:26pm UTC](https://discourse.itk.org/t/writing-dicom-file-through-using-imageseriesreader/2563/2 "2019-12-22T22:26:29Z")

</div>

Hello @tatsuaki,

Please provide the following details so that we can recreate your issue:

1. SimpleITK version.
2. Python version.
3. Operating system.
4. SimpleITK, binary distribution (conda or pip installed).

Please be aware that the latest SimpleITK release is 1.2.4 . Often, just upgrading to the newest release solves issues as these are continually addressed.

---

<div class="post-metadata">

**Author:** ![tatsuaki](https://discourse.itk.org/user_avatar/discourse.itk.org/tatsuaki/32/1153_2.png) [@tatsuaki](https://discourse.itk.org/u/tatsuaki)\
**Post date:** [December 23, 2019, 3:55am UTC](https://discourse.itk.org/t/writing-dicom-file-through-using-imageseriesreader/2563/3 "2019-12-23T03:55:22Z")

</div>

Hi, @zivy

Thank you for your reply.

My environment are,

SimpleITK version = 1.2.4  
libitk = 4.13.2  
Python version = 3.6.7  
Operating system = win10  
SimpleITK, binary distribution (conda or pip installed) = conda, I am using AnacondaNavigator 1.9.7.

---

<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:** [December 23, 2019, 3:33pm UTC](https://discourse.itk.org/t/writing-dicom-file-through-using-imageseriesreader/2563/4 "2019-12-23T15:33:36Z")

</div>

Hello @tatsuaki,

Generally speaking your code is fine (not sure why you have an `enumerate` whose value isn’t used when copying the tags).

Your issue is with the understanding of how a DICOM series is stored as a set of slices. Each slice is independently positioned in space ([image position patient](https://dicom.innolitics.com/ciods/ct-image/image-plane/00200032), 0020|0032). The spacing in the z axis is determined when reading all slices and is the difference between the z coordinates of consecutive slices. When you read a single slice there is no way to know what is the z-spacing, we only know that this slice is positioned in 3D so we arbitrarily set the spacing in Z to 1.

If you read the whole output series using a series reader you should see the correct spacing.

---

<div class="post-metadata">

**Author:** ![tatsuaki](https://discourse.itk.org/user_avatar/discourse.itk.org/tatsuaki/32/1153_2.png) [@tatsuaki](https://discourse.itk.org/u/tatsuaki)\
**Post date:** [December 23, 2019, 10:54pm UTC](https://discourse.itk.org/t/writing-dicom-file-through-using-imageseriesreader/2563/5 "2019-12-23T22:54:40Z")

</div>

Hello, @zivy

> If you read the whole output series using a series reader you should see the correct spacing.

Thank you inform nice trick. This way is seems nice to processing 3D spacing.

Finally, I decided to use the combination of the sitk(for resampling, filtering) and the pydicom(dcm in/out).  
Path way is 1.read series-sitk, 2.resampling/filtering-sitk, 3.to numpy array-sitk, 4.read original dcm(to use as holder) and edit metadata, replace pixel\_value, and then output -pydicom.

Thanks a lot.

tatsuaki

---

<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, 12:32pm UTC](https://discourse.itk.org/t/writing-dicom-file-through-using-imageseriesreader/2563/6 "2021-03-29T12:32:14Z")

</div>

Hi @tatsuaki

I do the same two pieces of code as above, the problem is that when I open the output folder in a DICOM viewer, it shows as a number of separate images and not as a single multi-slice image.  
Do you also have the same?
