You can create an uninitialized array first:
pixarray = (ctypes.c_int * (w_px * h_px))()
and then copy the contents of the image into it:
# dylib in MacOSX, cdll.wincrt in Win, libc.so.? in Unix, ... clib = ctypes.CDLL('libc.dylib') _ = clib.memcpy(pixarray, im.tostring(), w_px * h_px * 4)
The returned memcpy value is an address that you are not interested in, so I "swallowed" it by assigning it the name "single underscore" (which by convention means "it does not concern me";).
Edit : as @Mu Mind points out in a comment, the last snippet can be usefully simplified to use ctypes.memmove without having to go to the platform for clib to hang: just do
_ = ctypes.memmove(pixarray, im.tostring(), w_px * h_px * 4)
Alex martelli
source share