Is it possible to convert/cast a .NET SimpleITK Image to python SimpleITK Image

Is it possible to convert/cast a .NET SimpleITK Image to python SimpleITK Image ?
Has anyone successfully fused .NET c# SimpleITK images to python.
I have the main application in .NET where I use SimpleITK to perform image registration and a few other task , and want to use its results ( SimpleITK Image) as inputs to PyRadiomics which takes in SimpleITK Images but their python type.

I am using Python.NET to bridge between .NET and Python

Thank you in advance for any help and pointers

Hello @Ervis,

The cleanest approach is probably to write the file to disk on the C# side of things and read it on the Python side.

Having said that, if everything has to happen in memory, possibly try:

  1. Transfer the image data to a C# buffer (see this example).
  2. Transfer buffer to Python, assuming this is possible via Python.Net. Also transfer the origin, spacing and direction cosine information.
  3. Call numpy.frombuffer with the buffer.
  4. Call sitk.GetImageFromArray and then img.SetOrigin, img.SetSpacing, img.SetDirection.

Given the multiple memory copies used by the approach above it might be just as efficient to write and read from disk.

Hi Ziv

Thank you , that is the current approach I am using , writing the files to disk on the C# side and reading them in on the python side.
It is becoming a nightmare writing and reading from and disk.
My application receives dicom files from a dicom server , writes them to disk just so I can use SimpleITK, process the files in SimpleITK , write them to disk again, read them again from Python , process them and write again .
I am dying for some way to direct link the data via a memory stream or connection pipeline or casting of sorts if it exists .
Again thank you very much

Hi @Ervis,

Grasping at straws here, possibly define a RAM disk?

Setup is specific to your operating system, but using this approach will give you fast read-write while maintaining the standard ITK/SimpleITK code which assumes disk IO.

Not as portable as moving code around, but the same code will work on all machines, just faster on those configured with a RAM disk.

1 Like


Have you seen this issue for python net:

It looks very close to what you are looking for.

I will have a look and see thank you Ziv , blowekamp