void * C array to a Numpy array using Swig

Travis E. Oliphant oliphant.travis at ieee.org
Thu Jan 12 18:26:28 EST 2006


Krish wrote:
> Hello People
> 
> I hope I am On Topic. Anyways, here is my problem. Any insights would
> be really appreciated.

Posting to the numpy-discussion at lists.sourceforge.net  list would help 
generate more responses, I think.

> 
> I have wrapped a C IO module using SWIG -> Python Module. Suppose the
> name of the module is "imageio" and the reader function from the file
> is image_read() which returns an object ( "filled" C structure) with a
> lot of members.
> 
> Let
> 
> a = imageio.image_read("testfile.image")
> 
> Now a is the object which contains pointer to data of the image. ( void
> * pdata). ( a also contains the datatype which I need to typecast to
> get the data pointed by pdata). My question is how to access the data
> pointed by pdata in *Python*.

Yes, you are right that you need to use typemaps.  It's been awhile 
since I did this kind of thing, but here are some pointers.

1)  Look at Michael Sanner's typemaps at

http://www.scripps.edu/mb/olson/people/sanner/html/Python/NumericTypemaps/

Except for the CHAR version, these should work for NumPy by replacing 
Numeric/arrayobject.h with numpy/arrayobject.h

2) In full scipy there are typemaps for numpy arrays in 
cluster/src/swig_num.i

Look here...

http://projects.scipy.org/scipy/scipy/file/trunk/Lib/cluster/src/swig_num.i

This should help you get started with some examples.  Typemaps can be a 
little confusing at first, but they do make your interface a bit nicer.

-Travis




More information about the Python-list mailing list