Image Module Bug

J

Jim Bob

Hi,

I've got a problem with loading images using the Image Module. When I load
images and then try to pass them to my C++ module using tostring() I get the
following error:

TypeError: argument 1 must be string without null bytes, not str

After tearing my hair out I figured out that it was because there was full
black (0) in my image which was screwing up the NULL string terminator.

Here's my question. How can I get around this? Can I pass the raw image
data in another format? Can I use the encoder options on the tostring()
method? Anyone have any ideas?

This is also a problem when I am trying to do RGBA format because the alpha
channel is 0 in transparent places.

Thanks,
Peter
 
L

Larry Bates

You should be able to use the struct module to pack your
data into a binary structure, then pass that to the C++
module. I do this in quite a few programs I have that
need to pass this type of data to C, C++ routines.

HTH,
Larry Bates
Syscon, Inc.
 
R

Richard Oudkerk

I had this problem too a week ago. You should use "s#" with
PyArg_ParseTuple instead of "s". See

http://www.python.org/doc/1.5.2p2/ext/parseTuple.html

So instead of doing something like:

static PyObject *
spam_something(PyObject *self, PyObject *args) {
char *string;

if (!PyArg_ParseTuple(args, "s", &string))
return NULL;

you do this:

static PyObject *
spam_something(PyObject *self, PyObject *args)
char *string;
int length;

if (!PyArg_ParseTuple(args, "s#", &string, &length))
return NULL;

'length' will be length of the string passed from python.



Richard
 
P

Peter

I found a good way of doing this.

In my interface, I use "s#" instead of "s" to read a string along with its
length. This allows me to pass NULL characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,663
Latest member
josh5959

Latest Threads

Top