# Convert NVL Matrix to NumPy array

**URL:** https://discourse.itk.org/t/convert-nvl-matrix-to-numpy-array/711
**Category:** Beginner Questions
**Tags:** python, vnl, numpy
**Created:** [February 25, 2018, 7:00pm UTC](https://discourse.itk.org/t/convert-nvl-matrix-to-numpy-array/711 "2018-02-25T19:00:22Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![glemaitre](https://discourse.itk.org/user_avatar/discourse.itk.org/glemaitre/32/264_2.png) [@glemaitre](https://discourse.itk.org/u/glemaitre)
#### Post date: [February 25, 2018, 7:00pm UTC](https://discourse.itk.org/t/convert-nvl-matrix-to-numpy-array/711/1 "2018-02-25T19:00:22Z")

</div>

Just found out that I should not post to the mailing list anymore. So here is my question.

I am using the python wrapper of itk to manipulate. I checked the quick  
start guide (  
[http://itkpythonpackage.readthedocs.io/en/latest/Quick\_start\_guide.html#usage](http://itkpythonpackage.readthedocs.io/en/latest/Quick_start_guide.html#usage))  
to go from numpy to vnl matrices and vice-versa. However, I got into  
trouble when trying to use those utils with itk images. As a short example:

```
import numpy as np

img = np.random.random((100, 100, 100))
img_itk = itk.GetImageFromArray(img)

vnl_matrix = img_itk.GetDirection().GetVnlMatrix()
type(vnl_matrix)
vnl_matrix_fixedPython.vnl_matrix_fixedD_3_3
itk.GetArrayFromVnlMatrix(vnl_matrix)

TypeError: in method 'itkPyVnlD__GetArrayViewFromVnlMatrix', argument 1 of
type 'vnl_matrixD *'

```

So what would the proper way of converting this matrix into a numpy array  
or view.

---

<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: [February 26, 2018, 5:10am UTC](https://discourse.itk.org/t/convert-nvl-matrix-to-numpy-array/711/2 "2018-02-26T05:10:09Z")

</div>

Hi Guillaume,

Welcome to Discourse! 🙂 ☀

`img_itk.GetDirection().GetVnlMatrix()` returns a `vnl_matrix_fixed` instead of `vnl_matrix`, the type expected by `GetArrayFromVnlMatrix`, so try

```auto
vnl_matrix = img_itk.GetDirection().GetVnlMatrix().as_matrix()

```

HTH,  
Matt
