[Numpy-discussion] convert to numarray

Pete Shinners pete at shinners.org
Sun Jun 8 11:41:03 EDT 2003

in the pygame project i've had the ability to map images into Numeric 
data arrays onto image pixel data. This has worked excellently for me, 
but in the near future i'd also like to support numarray.

early in numarray's development it looked like this was not going to be 
possible at all. i've been following numarray loosely, and it sure looks 
like things have 'loosened' up a bit. still there are a few things i am 
doing that i'm unsure if numarray is ready to handle yet?

what is going to make this tricky is i'm doing a bit of 'attribute 
mangling' to the Numeric array structure. this is necessary as the image 
data is extremely 'non-flat'. also, since i am referencing data held in 
another python object, i need to make sure the array holds a reference 
to the original object. these are the things i'm afraid i'll be stuck on.

here is pretty much what i am doing now, simplified quite a bit...

PyObject* pixelsarray(SDL_Surface *surf)
     int dim[3];
     PyObject *array;

     dim[0] = surf->w;
     dim[1] = surf->h;
     dim[2] = 3;
     array = PyArray_FromDimsAndData(3, dim,
         PyArray_UBYTE, surf->pixels);
         PyArrayObject *a = (PyArrayObject*)array
         a->flags = OWN_DIMENSIONS|OWN_STRIDES;
         a->strides[2] = 1;
         a->strides[1] = surf->pitch;
         a->strides[0] = surf->format->BytesPerPixel;
         a->base = _pyobject_to_surf_;
     return array;

note that depending on pixel packing and endianess, the strides[2] can 
become -1. smiley. also pretend a->base is pointing to a real python 
object, which it does in the real version.

there is likely a way to workaround the "base" requirement with weakrefs 
i suppose, but i'd rather not jump through the extra hoops. the real 
necessity is setting the strides how i want. i didn't see any array 
creation functions that allow me to pick my own strides. once i create 
the array here i never change any of the array attributes.

if this looks doable then it's time for me to sit down with the numarray 
docs and see what new and exciting things await me :]

More information about the NumPy-Discussion mailing list