# SimpleITK WriteImage() fails \*silently\* when writing DICOM -- why? And what to do?

**URL:** <https://discourse.slicer.org/t/simpleitk-writeimage-fails-silently-when-writing-dicom-why-and-what-to-do/20369>\
**Category:** Development\
**Tags:** dicom, python\
**Created:** [October 26, 2021, 7:55pm UTC](https://discourse.slicer.org/t/simpleitk-writeimage-fails-silently-when-writing-dicom-why-and-what-to-do/20369 "2021-10-26T19:55:56Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![masonbogue](https://avatars.discourse-cdn.com/v4/letter/m/c77e96/32.png) [@masonbogue](https://discourse.slicer.org/u/masonbogue)\
**Post date:** [October 26, 2021, 11:47pm UTC](https://discourse.slicer.org/t/simpleitk-writeimage-fails-silently-when-writing-dicom-why-and-what-to-do/20369/7 "2021-10-26T23:47:14Z")

</div>

Hi,

After spending some more time working with this code, I can understand why you might have adopted a somewhat more whimsical tone than I was expecting. Being told over and over “writing DICOM files is very hard” sounded… I don’t know… like you thought I wasn’t trying. I was too short-tempered myself.

It turns out that the example needs very little modification. The current script is now short enough to post – the old one had _dozens_ of lines setting various DICOM attributes – and it looks like this:

```python

import os
import DICOMScalarVolumePlugin
import slicer, slicer.util

def convert_file(infile, outdir):
      fileName = os.path.basename(infile).split(".")[0]
      ptName = fileName[0:7]

      if not slicer.util.loadVolume(infile):
            print("ERROR: Cannot load MHD " + infile)
      volumeNode = slicer.util.getNode(fileName)

      # Create patient and study and put the volume under the study
      shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
      patientItemID = shNode.CreateSubjectItem(shNode.GetSceneItemID(), ptName)
      studyItemID = shNode.CreateStudyItem(patientItemID, fileName)
      volumeShItemID = shNode.GetItemByDataNode(volumeNode)
      shNode.SetItemParent(volumeShItemID, studyItemID)
      shNode.SetItemAttribute(studyItemID, "DICOM.Modality", "CT") # *this* didn't work
      shNode.SetItemAttribute(studyItemID, "DICOM.RescaleType", "HU") # neither did *this*

      import DICOMScalarVolumePlugin
      exporter = DICOMScalarVolumePlugin.DICOMScalarVolumePluginClass()
      exportables = exporter.examineForExport(volumeShItemID)
      for exp in exportables:
            exp.directory = outdir

      exporter.export(exportables)

```

So, now I can produce DICOM files! I was surprised at first by all of the long variable names, but in order to do simple things I don’t need to figure them all out.

Unfortunately, I seem to have reproduced this bug (cf. the commented lines above):

> [@Rescale type attribute when creating DICOM series](https://discourse.slicer.org/t/rescale-type-attribute-when-creating-dicom-series/3399):
>
> Hello, I’m using Slicer to create a downsampled CT series, and I was able to do that just fine, except that it appears the “Rescale type” attribute was changed from HU to US. Due to that I cannot export the images anymore in the other program I’m using (Treatment Planning Station). Is there any way to keep this attribute unchanged, or to revert it back to its original value ? Thanks ! Antoine

I know this is probably frustrating for Andreas, because he had put in a fix (thank you!) for it at lines 176-9 of this file:

> <https://github.com/Slicer/Slicer/blob/main/Modules/CLI/CreateDICOMSeries/CreateDICOMSeries.cxx>

Unfortunately, the fix doesn’t seem to apply for me, because when I try to import the file into the TPS, it complains about an invalid rescaleType, which is probably being set to “US” by GDCM.

So I think my new question is: **in what Slicer version is this fix found?** I am on Slicer 4.10.2. I may be able to convince IT to upgrade us. Is there any other way to set the rescaleType when exporting an image as DICOM? (Previously I did this by `image_slice.SetMetaData("0028|1054", "HU")` with a SimpleITK object)

Thanks for your help,  
Mason

---

_[View the full topic](https://discourse.slicer.org/t/simpleitk-writeimage-fails-silently-when-writing-dicom-why-and-what-to-do/20369)._
