# ITKNumpyBridge problems converting from numpy array.

**URL:** https://discourse.itk.org/t/itknumpybridge-problems-converting-from-numpy-array/222
**Category:** Beginner Questions
**Tags:** python
**Created:** [September 27, 2017, 1:05am UTC](https://discourse.itk.org/t/itknumpybridge-problems-converting-from-numpy-array/222 "2017-09-27T01:05:13Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [September 27, 2017, 1:05am UTC](https://discourse.itk.org/t/itknumpybridge-problems-converting-from-numpy-array/222/1 "2017-09-27T01:05:13Z")

</div>

I am using [ITKBridgeNumPy](https://github.com/InsightSoftwareConsortium/ITKBridgeNumPy) to pass an image to a numpy algorithm, and then returning it to another image.

```
    #image is a RealImageType
    X = itk.GetArrayViewFromImage(image)
    print("X", X.shape)
    lam = 0.1
    F = ptv.tv1_2d(X, lam)
    print("F", F.shape)
    # Line77 (error in next line)
    modifiedImage = itk.PyBuffer[RealImageType].GetImageViewFromArray(F) 

```

But I am getting an error that I don’t get, I thought `modifiedImage` doesn’t need any initialization (following ITKBridgeNumpy example).

```auto
X (1280, 1280)
F (1280, 1280)
RuntimeError: Size mismatch of image and Buffer.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "wavelet-TV.py", line 77, in <module>
    modifiedImage = itk.PyBuffer[RealImageType].GetImageViewFromArray(F)
  File "/home/phc/repository_local/total_variation/venv_proxTV/lib/python3.6/site-packages/itk/Configuration/../itkPyBufferPython.py", line 1486, in GetImageViewFromArray
    imgview = itkPyBufferIF2._GetImageViewFromArray( ndarr, ndarr.shape[::-1], 1)
  File "/home/phc/repository_local/total_variation/venv_proxTV/lib/python3.6/site-packages/itk/Configuration/../itkPyBufferPython.py", line 1400, in _GetImageViewFromArray
    return _itkPyBufferPython.itkPyBufferIF2__GetImageViewFromArray(arr, shape, numOfComponent)
SystemError: <built-in function itkPyBufferIF2__GetImageViewFromArray> returned a result with an error set

```

Any hint?

---

<div class="post-metadata">

### Author: ![phcerdan](https://discourse.itk.org/user_avatar/discourse.itk.org/phcerdan/32/286_2.png) [@phcerdan](https://discourse.itk.org/u/phcerdan)
#### Post date: [September 27, 2017, 2:55am UTC](https://discourse.itk.org/t/itknumpybridge-problems-converting-from-numpy-array/222/2 "2017-09-27T02:55:25Z")

</div>

Solved, types where different!

```auto
print(X.dtype) #float32
print(F.dtype) #float64

```

To solve:

```auto
F = F.astype(X.dtype, copy=False)

```

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [September 27, 2017, 6:22pm UTC](https://discourse.itk.org/t/itknumpybridge-problems-converting-from-numpy-array/222/3 "2017-09-27T18:22:20Z")

</div>

Cool!

Another approach is to use:

```auto
modifiedImage = itk.GetImageViewFromArray(F) 

```

instead of

```auto
modifiedImage = itk.PyBuffer[RealImageType].GetImageViewFromArray(F) 

```

because it automatically uses the correct type.

More information was provided by @fbudin here:

[https://blog.kitware.com/convert-itk-data-structures-to-numpy-arrays/](https://blog.kitware.com/convert-itk-data-structures-to-numpy-arrays/)
