Hello People
I hope I am On Topic. Anyways, here is my problem. Any insights would
be really appreciated.
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_r ead("testfile.i mage")
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*.
Ideally I want the data to be converted into a Scipy(Numpy) Array
object so that I can perform further operations in Python. Any pointers
will be appreciated.
I think this has to do with PyArrayObjects, TypeMaps etc. but I
couldn't consolidate the whole information. If anyone has done this
kinda stuff before or if anyone can point me to a correct/better way,
it would be really great.
Regards
Krish Subramaniam 5 7322
Krish,
In case you find a good solution, I am also looking for one!
For now I essentially use helper functions on the c side which wrap in
SWIG to return the data as a string in python. That string can then be
converted to a numpy array using the fromstring function. This is
inefficient as it does an unnecessary copy but avoids dependence on
numeric versus numarray etc. It uses the cstring thing in SWIG (see the
manual). The library I am wrapping does not have an image struct, but
returns the data into memory that the user has to malloc.
In the swig file I have something like this, which I've simplified to
try to get to the point. It assumes you have two c functions which take
a pointer to your struct as argument, the first returns the size of the
data (what to malloc), the second copies the data into your memory
where a pointer to the memory location was second arg.
Doubtless I've introduced typos below, but hopefully you get the idea?
Good luck,
Jon
---
typedef struct
{
stuff /* I don't know or care what is in here */
} imagefilestruct ;
%extend imagefilestruct {
[... snip constructor destructor other functions etc]
%cstring_output _allocate_size( char ** s, int *slen, free(*$1))
get_data ;
void get_data(char **s, int *slen){
void * array;
size_t size;
size = libraryfunction _get_size(self) ;
array=malloc(si ze));
libraryfunc_get _data(self, array);
*slen = size;
*s = (char *) array;
}
}
Thanks Jon Much
I will implement your method for the time being. I am sure there is
some method which doesn't copy the data but uses the existing data.
I mean essentially we should build a PyArrayObject with the data intact
and other parameters filled. If someone sheds some light, would be
awesome. I am gonna try it this weekend.
Appreciated
Krish
Krish wrote: Hello People
I hope I am On Topic. Anyways, here is my problem. Any insights would be really appreciated.
Posting to the nu************* *@lists.sourcef orge.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_r ead("testfile.i mage")
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/peop...mericTypemaps/
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/scip...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
"Travis E. Oliphant" <ol************ *@ieee.org> writes: Krish wrote:
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.
Also, there's http://geosci.uchicago.edu/csc/numptr
Philip Austin wrote: "Travis E. Oliphant" <ol************ *@ieee.org> writes:
Krish wrote:
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.
Also, there's http://geosci.uchicago.edu/csc/numptr
This is interesting.
I've noticed his getpointerX functions seem to be implemented already by
PyArray_AsCArra y in the new NumPy.
-Travis This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bo Peng |
last post by:
Dear list,
I am writing a Python extension module that needs a way to expose pieces
of a big C array to python. Currently, I am using NumPy like the following:
PyObject* res = PyArray_FromDimsAndData(1, int*dim, PyArray_DOUBLE,
char*buf);
Users will get a Numeric Array object and can change its values (and
actually change the underlying C array).
|
by: TG |
last post by:
Hi there !
I'm just starting to use Numeric here, and I'm wondering : how can I
efficiently initialize every values of a N-dimensional array, given I
don't know the number of dimensions ?
I'm looking for something like a map function, or a way to conveniently
iterate through the whole N-array, but I didn't find anything ... yet.
If anyone has a clue, I'm listening.
|
by: SpreadTooThin |
last post by:
Basically I think the problem is in converting from a 32 bit integer to
a 16 bit integer.
I have two arrays:
import array
a = array.array('L', )
b = array.array('H', )
b = a
|
by: Tartifola |
last post by:
Hi,
I'm working with numerical array and I'm a little lost on a particular
sorting of one of them. In particular I have an array like
a = array(,])
and I need to sort it using only the first column as reference but
keeping the lines together so to obtain
array(,
|
by: jimgardener |
last post by:
hi,
(i posted this to numpy discussion grp couple of days back ..but it
fails to appear..)since it is needed for my work i would appreciate if
anyone can help me with this question
i have two ndarrays of 1000 elements each and want to copy all
elements from srcarray to destarray
srcarray=numpy.array( )
| |
by: Sean Davis |
last post by:
I have a set of numpy arrays which I would like to save to a gzip
file. Here is an example without gzip:
b=numpy.ones(1000000,dtype=numpy.uint8)
a=numpy.zeros(1000000,dtype=numpy.uint8)
fd = file('test.dat','wb')
a.tofile(fd)
b.tofile(fd)
fd.close()
|
by: knielsen73 |
last post by:
Hi,
I am a python newbie, trying to convert my IDL scripts to python. I am
kind of stuck at the moment. I am reading in a 1-D data file with 2000
data points. I need to put them in a 3-D array with size . I
have defined the field array as arr = zeros((10,10,20)) but don't know
how to read the data into the array.
Also, I need to extract a slice of a 3-D array and tried a =
array_name(:,:,20) but that didn't work.
|
by: Rick Giuly |
last post by:
Hello All,
Case 1
This generates an error, which makes sense because the argument should
be a list of numbers:
numpy.array(10,10)
Case 2
This does not generate an error and the result is an array with a
single element:
|
by: Rüdiger Werner |
last post by:
Hello!
Out of curiosity and to learn a little bit about the numpy package i've
tryed to implement
a vectorised version of the 'Sieve of Zakiya'.
While the code itself works fine it is astounding for me that the numpy
Version is almost 7 times slower than
the pure python version. I tryed to find out if i am doing something wrong
but wasn't able to find any answer.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |