efficiently convert string (or tuple) to ctypes array

Posted by Mu Mind on Stack Overflow See other posts from Stack Overflow or by Mu Mind
Published on 2010-05-29T15:27:22Z Indexed on 2010/05/29 15:32 UTC
Read the original article Hit count: 189

Filed under:
|
|

I've got code that takes a PIL image and converts it to a ctypes array to pass out to a C function:

w_px, h_px = img.size
pixels = struct.unpack('%dI'%(w_px*h_px), img.convert('RGBA').tostring())
pixels_array = (ctypes.c_int * len(pixels))(*pixels)

But I'm dealing with big images, and unpacking that many items into function arguments seems to be noticeably slow. What's the simplest thing I can do to get a reasonable speedup?

I'm only converting to a tuple as an intermediate step, so if it's unnecessary, all the better.

© Stack Overflow or respective owner

Related posts about python

Related posts about pil