[PYTHON IMAGE-SIG] NumPy and PIL extension module

Zachary_Roadhouse@brown.edu Zachary_Roadhouse@brown.edu
Sat, 03 May 1997 23:57:05 -0400 (EDT)

Hello!  With exams nearly over, I am attempting to delve into writing
Python extension modules.  I would like to write a module nPIL which has
two functions in it for converting between PyArrayObject and Imaging

array ImageToArray(image)
image ArrayToImage(array)

I have a module written in python that does what I'm asking but at the
expense of converting to a string first.  It also doesn't handle
multi-banded images (just grey scale).

I'm running into problems in figuring out what type I should use for the
array and how to set the (x,y,colorplanes...) in the array.  Here is what
I have so far:

static PyObject*
ImageToArray(PyObject* args)
  PyObject* op;
  Imaging im;

  /* The image can have up to three dimensions */
  /* x,y and number of color planes            */
  int dims[3];

  PyObject* array;

  UINT8 *p;

  /* 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 */
  im = PyImaging_AsImaging(op);
  if (!im)
    return NULL;

  /* Set the dimensions of the array to be the */
  /* x and y dimensions of the image           */
  dims[0] = im->xsize;
  dims[1] = im->ysize;

  /* single layer */
  if(im->image8) {
    int value;

    array = PyArray_FromDims(2,dims,PyArray_INT);

    value = im->image8[y][x];


  /* multilayer */
  else {
    if(im->bands == 3) {
      dims[2] = 3;
      array = PyArray_FromDims(3,dims,PyArray_INT);

      p = (UINT8*) &im->image32[y][x];

     - Zack

E-MAIL: Zachary_Roadhouse@brown.edu  WEB: http://althor.netspace.org/~zack/
Brown University, Box 220, Providence, RI 02912
Phone: (401) 863 - 5435

IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org