# Is it possible to get a writable array view of a SimpleITK image?

**URL:** https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585
**Category:** Beginner Questions
**Tags:** python, numpy, simpleitk
**Created:** [February 8, 2019, 1:58pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585 "2019-02-08T13:58:36Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![david-hoffman](https://discourse.itk.org/user_avatar/discourse.itk.org/david-hoffman/32/598_2.png) [@david-hoffman](https://discourse.itk.org/u/david-hoffman)
#### Post date: [February 8, 2019, 1:58pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/1 "2019-02-08T13:58:37Z")

</div>

Following up on this question: [https://stackoverflow.com/questions/54557586/does-simpleitk-have-broadcasting/54577094](https://stackoverflow.com/questions/54557586/does-simpleitk-have-broadcasting/54577094)

If `GetArrayViewFromImage` returned a writable view then you could use `numpy` broadcasting without issue. Is there a way to do this?

---

<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: [February 8, 2019, 2:21pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/2 "2019-02-08T14:21:26Z")

</div>

Just to emphasize, ignoring the spatial concept of an image in SimpleITK is not recommended. Having said that, you can still do what you want (would recommend that you add a comment in your code warning yourself that this is a dangerous thing to do):

```auto
import SimpleITK as sitk

img = sitk.ReadImage('training_001_ct.mha')
slc = sitk.GridSource(outputPixelType=img.GetPixelID(), size=img.GetSize()[0:2], 
                             sigma=(0.1,0.1), gridSpacing=(20.0,20.0))

img_view = sitk.GetArrayViewFromImage(img)
slc_view = sitk.GetArrayViewFromImage(slc)

new_vol = img_view*slc_view[None,:]

sitk.Show(sitk.GetImageFromArray(new_vol))

```

---

<div class="post-metadata">

### Author: ![david-hoffman](https://discourse.itk.org/user_avatar/discourse.itk.org/david-hoffman/32/598_2.png) [@david-hoffman](https://discourse.itk.org/u/david-hoffman)
#### Post date: [February 8, 2019, 2:28pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/3 "2019-02-08T14:28:13Z")

</div>

Unfortunately, that won’t work as I’m trying to avoid memory copies of large arrays.

---

<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: [February 8, 2019, 2:38pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/4 "2019-02-08T14:38:35Z")

</div>

Not sure that I understand the issue. Is the problem with the creation of `new_vol` instead of the overwriting of `img_view`? The only other copy is in `sitk.GetImageFromArray(new_vol))`.

---

<div class="post-metadata">

### Author: ![david-hoffman](https://discourse.itk.org/user_avatar/discourse.itk.org/david-hoffman/32/598_2.png) [@david-hoffman](https://discourse.itk.org/u/david-hoffman)
#### Post date: [February 8, 2019, 3:34pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/5 "2019-02-08T15:34:49Z")

</div>

Yeah the `GetImageFromArray` is the copy I’m worried about. The issues is some of the images a 50 GB+ so I’d like to make as few copies as possible.

---

<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: [February 8, 2019, 3:41pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/6 "2019-02-08T15:41:08Z")

</div>

Got it. Unfortunately, I don’t have a good solution off the top of my head. If I have an idea how to get around this will post it here.

---

<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: [February 8, 2019, 3:43pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/7 "2019-02-08T15:43:25Z")

</div>

You can look at the implementation of `GetImageFromArray`:

> <https://github.com/SimpleITK/SimpleITK/blob/4aabd77bddf508c1d55519fbf6002180a08f9208/Wrapping/Python/Python.i#L764-L794>

We have maintained a very conservative approach to managing array and references, which makes the interfaces quite stable. If you look at the implementation you can see it creates a new `Image` then fills it. I think we could very easily take an image as an argument and then “fill” it with the nparray. This would both be more efficient and very safe.

Would this help your situation?

---

<div class="post-metadata">

### Author: ![david-hoffman](https://discourse.itk.org/user_avatar/discourse.itk.org/david-hoffman/32/598_2.png) [@david-hoffman](https://discourse.itk.org/u/david-hoffman)
#### Post date: [February 8, 2019, 4:04pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/8 "2019-02-08T16:04:04Z")

</div>

When you say “fill” do you mean that both the ndarray object and the sitk.Image object would be looking at the same location in memory?

---

<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: [February 8, 2019, 6:21pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/9 "2019-02-08T18:21:31Z")

</div>

By `fill` I do mean copy. If you reuse the input image, you’ll still create the output numpy array but you can then copy back to the same location as the input.

---

<div class="post-metadata">

### Author: ![david-hoffman](https://discourse.itk.org/user_avatar/discourse.itk.org/david-hoffman/32/598_2.png) [@david-hoffman](https://discourse.itk.org/u/david-hoffman)
#### Post date: [February 8, 2019, 6:46pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/10 "2019-02-08T18:46:36Z")

</div>

Yes that would work for me.

---

<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: [February 8, 2019, 7:30pm UTC](https://discourse.itk.org/t/is-it-possible-to-get-a-writable-array-view-of-a-simpleitk-image/1585/11 "2019-02-08T19:30:13Z")

</div>

Can you please create a feature request on [GitHub](https://github.com/SimpleITK/SimpleITK/issues) to add an “outputImage” to the `GetImageFromArray` method.

I think the method could look something like this:

def GetImageFromArray( arr, isVector=None, outputImage=None):  
“”“Get a SimpleITK Image from a numpy array. If isVector is True, then the Image will have a Vector pixel type, and the last dimension of the array will be considered the component index. By default when isVector is None, 4D images are automatically considered 3D vector images. If outpuImage is set then it will contain the output and the outputImage’s buffer may be reused. “””

```
if not HAVE_NUMPY:
    raise ImportError('Numpy not available.')

z = numpy.asarray( arr )

if isVector is None:
  if z.ndim == 4:
    isVector = True

if isVector:
  id = _get_sitk_vector_pixelid( z )
  if z.ndim > 2:
    number_of_components = z.shape[-1]
    shape = z.shape[-2::-1]
  else:
    number_of_components = 1
    shape = z.shape[::-1]
else:
  number_of_components = 1
  id = _get_sitk_pixelid( z )
  shape = z.shape[::-1]

if (outputImage and
    outputImage.GetSize() == shape and
    outputImage.GetNumberOfComponentsPerPixel() == number_of_components and
    outputImage.GetPixelIDValue() == id):
  img = outputImage
  print("Reusing outputImage's buffer!")
else:
  # SimpleITK throws an exception if the image dimension is not supported
  img = Image( shape, id, number_of_components )

sitk.SimpleITK._SetImageFromArray( z.tostring(), img )

return img

```

```auto

You might have to import some other things from SimpleITK to get it to work out of [this context](https://github.com/SimpleITK/SimpleITK/blob/4aabd77bddf508c1d55519fbf6002180a08f9208/Wrapping/Python/Python.i#L764-L794). But I think you can use it to get the job done.
```
