[Numpy-discussion] Quicker image transfer, tobuffer?

Filip Wasilewski filip at ftv.pl
Tue Jul 11 16:31:56 CDT 2006

Hi Travis,

this is a great example of the __array_interface__ usage.

I have spotted some problems after patching the Image.py module and
trying to display an array created from Image in matplotlib.

First issue is a minor one. There is a difference in axis order between
ndarray and PIL:

def _conv_type_shape(im):
    shape = im.size[::-1]
    typ, extra = _MODE_CONV[im.mode]
    if extra is None:
        return shape, typ
    shape += (extra,)
    return shape, typ

The second seems to be more complex and may be a more general. The
memory of string created by self.tostring() seems to be deallocated
before array is created (v, win).
Everything works fine after storing the reference to data, but this
probably should be done somewhere else:

     def __get_array_interface__(self):
         new = {}
         shape, typestr = _conv_type_shape(self)
         new['shape'] = shape
         new['typestr'] = typestr
         new['data'] = self.tostring()
         self._str_data = new['data']    # a dirty hack
         return new


More information about the Numpy-discussion mailing list