Numpy array from itk image

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, 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/”, line 257, in GetArrayFromImage
return _GetArrayFromImage(imageOrFilter, “GetArrayFromImage”, keepAxes, updateLargestPossibleRegion)
File “/Users/srit/miniconda3/envs/testitk/lib/python3.6/site-packages/”, 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/”, 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/
VERSION = ‘5.0a02.dev20180704+135.ga3b74bc’
(testitk) OS-SR-466:~ srit$ python --version
Python 3.6.6 :: Anaconda, Inc.

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:

Please try the nightly builds over the next few days.

1 Like

Yes, I confirm it fixes it. Thanks!

1 Like