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

**URL:** https://discourse.itk.org/t/is-it-possible-to-convert-cast-a-net-simpleitk-image-to-python-simpleitk-image/4728
**Category:** Beginner Questions
**Created:** [January 13, 2022, 8:18pm UTC](https://discourse.itk.org/t/is-it-possible-to-convert-cast-a-net-simpleitk-image-to-python-simpleitk-image/4728 "2022-01-13T20:18:49Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ervis](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@Ervis](https://discourse.itk.org/u/Ervis)
#### Post date: [January 13, 2022, 8:18pm UTC](https://discourse.itk.org/t/is-it-possible-to-convert-cast-a-net-simpleitk-image-to-python-simpleitk-image/4728/1 "2022-01-13T20:18:49Z")

</div>

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](http://Python.NET) to bridge between .NET and Python

Thank you in advance for any help and pointers

---

<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: [January 13, 2022, 8:56pm UTC](https://discourse.itk.org/t/is-it-possible-to-convert-cast-a-net-simpleitk-image-to-python-simpleitk-image/4728/2 "2022-01-13T20:56:33Z")

</div>

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](https://simpleitk.readthedocs.io/en/master/link_CSharp_docs.html)).
2. Transfer buffer to Python, assuming this is possible via [Python.Net](http://Python.Net). Also transfer the origin, spacing and direction cosine information.
3. Call [numpy.frombuffer](https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html) 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.

---

<div class="post-metadata">

### Author: ![Ervis](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@Ervis](https://discourse.itk.org/u/Ervis)
#### Post date: [January 13, 2022, 9:14pm UTC](https://discourse.itk.org/t/is-it-possible-to-convert-cast-a-net-simpleitk-image-to-python-simpleitk-image/4728/3 "2022-01-13T21:14:47Z")

</div>

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

---

<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: [January 13, 2022, 9:53pm UTC](https://discourse.itk.org/t/is-it-possible-to-convert-cast-a-net-simpleitk-image-to-python-simpleitk-image/4728/4 "2022-01-13T21:53:42Z")

</div>

Hi @Ervis,

Grasping at straws here, possibly define a [RAM disk](https://en.wikipedia.org/wiki/RAM_drive)?

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.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [January 14, 2022, 5:10pm UTC](https://discourse.itk.org/t/is-it-possible-to-convert-cast-a-net-simpleitk-image-to-python-simpleitk-image/4728/5 "2022-01-14T17:10:04Z")

</div>

Hello,

Have you seen this issue for python net:

> <https://github.com/pythonnet/pythonnet/pull/980>
>
> \### What does this implement/fix? Explain your changes.
> 
> Passing data like ima…ges from managed to python will be easyer than ever!
> I implemented the python buffer api which makes copying big chunks of data directly into the python object buffer possible!
> 
> You can copy strings to python object:
> 
> PyObject array = scope.Eval("bytearray(3)");
> byte\[\] managedArr = new UTF8Encoding().GetBytes(new char\[\] { 'a', 'b', 'c' });
> 
> PyBuffer buf = array.GetBuffer(out bool success, 0);
> if (success)
> {
> buf.WriteToBuffer(managedArr, 0, managedArr.Length);
> buf.Release();
> }
> 
> Or copy images to python byte arrays:
> 
> byte\[\] imageArr = LoadImage();
> PyObject array = scope.Eval("bytearray(" + managedArr.Length + ")");
> 
> PyBuffer buf = array.GetBuffer(out bool success, 0);
> if (success)
> {
> buf.WriteToBuffer(managedArr, 0, managedArr.Length);
> buf.Release();
> }
> 
> \### Does this close any currently open issues?
> 
> No
> 
> \### Any other comments?
> 
> ...
> 
> \### Checklist
> 
> Check all those that are applicable and complete.
> 
> \- \[x\] Make sure to include one or more tests for your change
> \- \[x\] If an enhancement PR, please create docs and at best an example
> \- \[x\] Add yourself to \[\`AUTHORS\`\](../blob/master/AUTHORS.md)
> \- \[x\] Updated the \[\`CHANGELOG\`\](../blob/master/CHANGELOG.md)

It looks very close to what you are looking for.

---

<div class="post-metadata">

### Author: ![Ervis](https://discourse.itk.org/letter_avatar_proxy/v4/letter/e/6de8d8/32.png) [@Ervis](https://discourse.itk.org/u/Ervis)
#### Post date: [January 14, 2022, 6:13pm UTC](https://discourse.itk.org/t/is-it-possible-to-convert-cast-a-net-simpleitk-image-to-python-simpleitk-image/4728/6 "2022-01-14T18:13:33Z")

</div>

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