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

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:


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):

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:

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