[Numpy-discussion] PyArray_BASE equivalent in python

Robert Kern robert.kern at gmail.com
Tue Nov 26 16:46:21 EST 2013


On Tue, Nov 26, 2013 at 9:37 PM, Peter Rennert <p.rennert at cs.ucl.ac.uk>
wrote:
>
> I probably did something wrong, but it does not work how I tried it. I
> am not sure if you meant it like this, but I tried to subclass from
> ndarray first, but then I do not have access to __array_interface__. Is
> this what you had in mind?
>
> from PySide import QtGui
> import numpy as np
>
> class myArray():
>      def __init__(self, shape, bits, strides):

You need to pass in the image as well and keep a reference to it.

>          self.__array_interface__ = \
>              {'data': bits,
>               'typestr': '<i32',
>               'descr': [('', '<f8')],
>               'shape': shape,
>               'strides': strides,
>               'version': 3}

Most of these are wrong. Something like the following should suffice:


class QImageArray(object):
    def __init__(self, qimage):
        shape = (qimage.height(), qimage.width(), -1)
        # Generate an ndarray from the image bits and steal its
        # __array_interface__ information.
        arr = np.frombuffer(qimage.bits(), dtype=np.uint8).reshape(shape)
        self.__array_interface__ = arr.__array_interface__
        # Keep the QImage alive.
        self.qimage = qimage

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20131126/23e3d30c/attachment.html>


More information about the NumPy-Discussion mailing list