# Numpy array from itk image

**URL:** <https://discourse.itk.org/t/numpy-array-from-itk-image/1097>\
**Category:** Engineering\
**Created:** [July 5, 2018, 9:02pm UTC](https://discourse.itk.org/t/numpy-array-from-itk-image/1097 "2018-07-05T21:02:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [July 5, 2018, 9:02pm UTC](https://discourse.itk.org/t/numpy-array-from-itk-image/1097/1 "2018-07-05T21:02:24Z")

</div>

With the latest ITK python packages, I cannot get a numpy array from an itk::Image. I have tried with version 5.0a02 and the [nightly packages](https://github.com/InsightSoftwareConsortium/ITKPythonPackage/releases/download/latest), I have

> \>\>\> import itk  
> \>\>\> img = itk.Image[itk.F,3].New()  
> \>\>\> region = img.GetLargestPossibleRegion()  
> \>\>\> region.SetSize([128,128,128])  
> \>\>\> img.SetLargestPossibleRegion(region)  
> \>\>\> img.Allocate()  
> \>\>\> itk.GetArrayFromImage(img)  
> Traceback (most recent call last):  
> File “”, line 1, in   
> File “/Users/srit/miniconda3/envs/testitk/lib/python3.6/site-packages/itkExtras.py”, line 257, in GetArrayFromImage  
> return \_GetArrayFromImage(imageOrFilter, “GetArrayFromImage”, keepAxes, updateLargestPossibleRegion)  
> File “/Users/srit/miniconda3/envs/testitk/lib/python3.6/site-packages/itkExtras.py”, line 246, in \_GetArrayFromImage  
> keys = [k for k in itk.PyBuffer.keys() if k[0] == output(imageOrFilter).\_\_class\_\_]  
> File “/Users/srit/miniconda3/envs/testitk/lib/python3.6/site-packages/itkLazy.py”, line 40, in \_\_getattribute\_\_  
> value = types.ModuleType.\_\_getattribute\_\_(self, attr)  
> AttributeError: module ‘itk’ has no attribute ‘PyBuffer’

Can you reproduce this? Any clue what’s wrong? This is tested with a miniconda environment:

> (testitk) OS-SR-466:~ srit$ pip list  
> Package Version
> 
> * * *
> 
> certifi 2018.4.16  
> itk 5.0a2.dev20180704  
> itk-core 5.0a2.dev20180704  
> itk-filtering 5.0a2.dev20180704  
> itk-io 5.0a2.dev20180704  
> itk-numerics 5.0a2.dev20180704  
> itk-registration 5.0a2.dev20180704  
> itk-segmentation 5.0a2.dev20180704  
> numpy 1.14.5  
> pip 10.0.1  
> setuptools 39.2.0  
> wheel 0.31.1  
> (testitk) OS-SR-466:~ srit$ head -1 /Users/srit/miniconda3/envs/testitk/lib/python3.6/site-packages/itkVersion.py  
> VERSION = ‘5.0a02.dev20180704+135.ga3b74bc’  
> (testitk) OS-SR-466:~ srit$ python --version  
> Python 3.6.6 :: Anaconda, Inc.

---

<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:** [July 5, 2018, 10:11pm UTC](https://discourse.itk.org/t/numpy-array-from-itk-image/1097/2 "2018-07-05T22:11:47Z")

</div>

Hi @simon.rit,

Thanks for the note. We migrated the NumPy bridge into the repository for ITK 5 and made some improvements. In the process the module name changed. This pull request should fix the issue:

> <https://github.com/InsightSoftwareConsortium/ITKPythonPackage/pull/106>

Please try the nightly builds over the next few days.

---

<div class="post-metadata">

**Author:** ![simon.rit](https://discourse.itk.org/letter_avatar_proxy/v4/letter/s/f08c70/32.png) [@simon.rit](https://discourse.itk.org/u/simon.rit)\
**Post date:** [July 9, 2018, 2:24pm UTC](https://discourse.itk.org/t/numpy-array-from-itk-image/1097/3 "2018-07-09T14:24:37Z")

</div>

Yes, I confirm it fixes it. Thanks!
