[Image-SIG] PIL video capture extension type problem

Fredrik Lundh fredrik@pythonware.com
Sun, 10 Feb 2002 11:59:23 +0100

Rich Drewes wrote:

> 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

PIL uses three different objects:

    - Image.Image instances (public objects)
    - ImagingObject (used inside the _imaging wrapper)
    - Imaging structures (used inside libImaging)

your Python code manipulates Image.Image instances; your C code
will want to manipulate Imaging structures.

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

here, "im" is an Image.Image instance.  it's a python object,
not an ImagingObject.

"im.im" is the ImagingObject, "im.im.id" is a pointer to the
Imaging structure, cast to a long integer.

("im.im" might be None, if the raster image hasn't been loaded.
if you cannot be sure that the image was created by a call to
Image.new, call "im.load()" before accessing the "im.im" attribute)

and yes, if you're going to fill the entire image anyway, you can
pass in None as the third argument (fill colour):

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

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

    # capture a frame into 'im':
    # im.load() # not needed if you're using Image.new

>    /* 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;

    long imvalue;
    if (!PyArg_ParseTuple(args, "l", &imvalue))
        return NULL;

    im = (Imaging) imvalue;

also see the snap function in Sane/sane.py and Sane/_sanemodule.c

hope this helps!

cheers /F