[Image-SIG] PIL video capture extension type problem

Rich Drewes drewes@interstice.com
Sat, 09 Feb 2002 11:34:00 -0800


Hello,

I'm trying to write a C extension that simply takes a PIL image as an 
argument and fills it in with data grabbed from a video capture device. 
 This should be simple, but the Python type casting is baffling me.  The 
Python code looks like this:

----
#!/usr/bin/python

import Image
import mouse

print "running"

# open the camera device:
mouse.init(0)

im=Image.new("RGB", (160,120))

# capture a frame into 'im':
mouse.getimage(im);
----

This fails at runtime on the last line with this error:

----
Traceback (most recent call last):
  File "mintest.py", line 14, in ?
    mouse.getimage(im);
SystemError: ././_imaging.c:155: bad argument to internal function
----

My extension code for the getimage() looks like this:

----
static PyObject *GetImage(PyObject *self, PyObject *args)
{
    PyObject* op;
    Imaging im;

    /* Check to see that one arguement was passed in */
    if (!PyArg_ParseTuple(args, "O", &op))
        return NULL;

    /* Check to see if that object is an Imaging */
    // this is how it's supposed to be done, I think:
    im = PyImaging_AsImaging(op);
    if (!im)
        return NULL;

    printf("in GetImage, type 0x%x x %d y %d\n", im->type, im->xsize, 
im->ysize);

    return Py_BuildValue("i", 1);
}
----

Note that I'm not actually trying to fil any video data into the Imaging 
structure yet.  I'll do that once it accepts that the variable 'im' is 
actually of the correct type!

The SystemError in _imaging.c:155 is apparently in the 
PyImaging_AsImaging() call, which apparently is supposed to recast the 
PyObject as an Imaging type.  However, the function is detecting some 
problem doing this recasting.  Why?

Thanks for any help,
Rich