Re: How to get (void*) data into a Python array

Trevor Bentley <[email protected]>
Newsgroups gmane.comp.python.pyobjc.devel
Message-ID <[email protected]>
> IMHO your problem is not PyObjc - you have everything you need there. It's getting things into NumPy.
> 
> One way would be to use ctypes. You can create a pointer to whatever data-type you want, and initialize that with the returned integer. I don't have example code right here (wrong machine), but it's not that hard.
> 
> then at least you can access the data properly, and stuff it into a NumPy array.
> 
> However, the best solution would of course be to be able to create a NumPy array based on the pointer. I know NumPy has a C-API, maybe that's exposed somehow?
> 
> Diez

That's where I took it immediately after sending the e-mail: ctypes
pointers.  I managed to get a pointer to something... maybe it's pixel
data.  NumPy doesn't want to import my pointer as an array, and it turns
out I don't know what format the returned data is in, anyway.

Since efficiency isn't even a slight concern, I actually found a
technique that works... but the final step is Cocoa writing the image to
a file.

This saves a bitmap using Cocoa:
--------------------------------------------------
ciimage = CIImage.imageWithCVImageBuffer_(videoFrame)
rep = NSCIImageRep.imageRepWithCIImage_(ciimage)
bitrep = NSBitmapImageRep.alloc().initWithCIImage_(ciimage)
bitdata = bitrep.representationUsingType_properties_(NSBMPFileType,
objc.NULL)
bitdata.writeToFile_atomically_("grab.bmp", False)
--------------------------------------------------

But the real answer just came by mistake while poking around in pdb.
'bitdata' in the above code is an NSData object, which PyObjC can
apparently magically copy over to Python-land as a read-only buffer:

--------------------------------------------------
bitbuf = bitdata.bytes()
f = open("python.bmp", "w")
f.write(bitbuf)
f.close()
--------------------------------------------------

Strange but true, native Python saves it faster on my machine:

Save bitdata with Cocoa's writeToFile:atomically: takes 0.077 s
Save bitdata with Python's write() takes 0.0288 s

Thanks for the help, I think I have everything I need now.

Trevor

------------------------------------------------------------------------------
The modern datacenter depends on network connectivity to access resources
and provide services. The best practices for maximizing a physical server's
connectivity to a physical network are well understood - see how these
rules translate into the virtual world? 
http://p.sf.net/sfu/oracle-sfdevnlfb
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.