# convert VTK array to numpy array

**URL:** https://discourse.vtk.org/t/convert-vtk-array-to-numpy-array/3152
**Category:** Support
**Created:** [April 25, 2020, 7:30am UTC](https://discourse.vtk.org/t/convert-vtk-array-to-numpy-array/3152 "2020-04-25T07:30:21Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Deepa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/deepa/32/1680_2.png) [@Deepa](https://discourse.vtk.org/u/Deepa)
#### Post date: [April 25, 2020, 7:30am UTC](https://discourse.vtk.org/t/convert-vtk-array-to-numpy-array/3152/1 "2020-04-25T07:30:21Z")

</div>

I have a VTKArray  
`VTKArray([-3.17113447, -16.9386692 , 16.73578644], dtype=float32)`  
I’m trying to convert this to a numpy array

`from vtk.util.numpy_support import vtk_to_numpy`

`vtk_to_numpy(edgeNode0_pos)`  
error:  
(self. **class**. **name** , name))  
AttributeError: ‘VTKArray’ object has no attribute ‘GetDataType’

Any help on how to resolve this error will be useful.

---

<div class="post-metadata">

### Author: ![banesullivan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/banesullivan/32/7143_2.png) [@banesullivan](https://discourse.vtk.org/u/banesullivan)
#### Post date: [April 27, 2020, 11:45am UTC](https://discourse.vtk.org/t/convert-vtk-array-to-numpy-array/3152/2 "2020-04-27T11:45:15Z")

</div>

That `VTKArray` is a numpy array - its a helper class that wraps the `numpy.ndarray` class. `vtk.util.numpy_support.vtk_to_numpy` converts `vtkDataArray`s into `VTKArrays`.

Try:

```python
isinstance(edgeNode0_pos, np.ndarray)

```

---

<div class="post-metadata">

### Author: ![arfe](https://discourse.vtk.org/user_avatar/discourse.vtk.org/arfe/32/3502_2.png) [@arfe](https://discourse.vtk.org/u/arfe)
#### Post date: [June 2, 2021, 2:58pm UTC](https://discourse.vtk.org/t/convert-vtk-array-to-numpy-array/3152/3 "2021-06-02T14:58:50Z")

</div>

I would like to convert the VTKArray into a numpy array anyway. Like a proper numpy array, no wrappers. Is that possible? I don’t understand how isinstance helps here.

Sorry for piggybacking on this old question, it just exactly describes what I’m looking for.

---

<div class="post-metadata">

### Author: ![zhang-qiang-github](https://discourse.vtk.org/user_avatar/discourse.vtk.org/zhang-qiang-github/32/2519_2.png) [@zhang-qiang-github](https://discourse.vtk.org/u/zhang-qiang-github)
#### Post date: [June 2, 2021, 4:14pm UTC](https://discourse.vtk.org/t/convert-vtk-array-to-numpy-array/3152/4 "2021-06-02T16:14:47Z")

</div>

The following two functions may help.

```auto
def vtkToNumpy(data):
    temp = vtk_to_numpy(data.GetPointData().GetScalars())
    dims = data.GetDimensions()
    component = data.GetNumberOfScalarComponents()
    if component == 1:
        numpy_data = temp.reshape(dims[2], dims[1], dims[0])
        numpy_data = numpy_data.transpose(2,1,0)
    elif component == 3 or component == 4:
        if dims[2] == 1: # a 2D RGB image
            numpy_data = temp.reshape(dims[1], dims[0], component)
            numpy_data = numpy_data.transpose(0, 1, 2)
            numpy_data = np.flipud(numpy_data)
        else:
            raise RuntimeError('unknow type')
    return numpy_data

def numpyToVTK(data, multi_component=False, type='float'):
    '''
    multi_components: rgb has 3 components
    type：float or char
    '''
    if type == 'float':
        data_type = vtk.VTK_FLOAT
    elif type == 'char':
        data_type = vtk.VTK_UNSIGNED_CHAR
    else:
        raise RuntimeError('unknown type')
    if multi_component == False:
        if len(data.shape) == 2:
            data = data[:, :, np.newaxis]
        flat_data_array = data.transpose(2,1,0).flatten()
        vtk_data = numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=data_type)
        shape = data.shape
    else:
        assert len(data.shape) == 3, 'only test for 2D RGB'
        flat_data_array = data.transpose(1, 0, 2)
        flat_data_array = np.reshape(flat_data_array, newshape=[-1, data.shape[2]])
        vtk_data = numpy_to_vtk(num_array=flat_data_array, deep=True, array_type=data_type)
        shape = [data.shape[0], data.shape[1], 1]
    img = vtk.vtkImageData()
    img.GetPointData().SetScalars(vtk_data)
    img.SetDimensions(shape[0], shape[1], shape[2])
    return img

```

---

<div class="post-metadata">

### Author: ![okahilak](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/o/7993a0/32.png) [@okahilak](https://discourse.vtk.org/u/okahilak)
#### Post date: [January 12, 2022, 1:14pm UTC](https://discourse.vtk.org/t/convert-vtk-array-to-numpy-array/3152/5 "2022-01-12T13:14:55Z")

</div>

Converting the VTKArray into a proper numpy array can done as follows:

```auto
>>> points
VTKArray([[7.881, 99.893, 31.901]], dtype=float32)
>>> import numpy as np
>>> np.array(points)
array([[7.881, 99.893, 31.901]], dtype=float32)

```
