Re: [Pyobjc-dev] How to get (void*) data into a Python array
Brought to you by:
ronaldoussoren
From: Trevor B. <mr...@gm...> - 2011-02-04 20:55:16
|
> 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 |