On Tue, Nov 26, 2013 at 1:37 PM, Peter Rennert <[email protected]> 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):
>          self.__array_interface__ = \
>              {'data': bits,
>               'typestr': '<i32',
>               'descr': [('', '<f8')],
>               'shape': shape,
>               'strides': strides,
>               'version': 3}

You need this object to also hold a reference to the image object --
the idea is that so long as the array lives it will hold a ref to this
object in .base, and then this object holds the image alive. But...

> image = QtGui.QImage('/home/peter/code/pyTools/sandbox/images/faceDemo.jpg')
>
> b = myArray((image.width(), image.height()), image.bits(),
> (image.bytesPerLine(), 4))
> b = np.asarray(b)
>
> b.base
> #<read-write buffer ptr 0x7fd744c4b010, size 1478400 at 0x264e9f0>

...this isn't promising, since it suggests that numpy is cleverly
cutting out the middle-man when you give it a buffer object, since it
knows that buffer objects are supposed to actually take care of memory
management.

You might have better luck using the raw pointer two-tuple form for
the "data" field. You can't get these pointers directly from a buffer
object, but numpy will give them to you. So you can use something like

  "data": np.asarray(bits).__array_interface__["data"]

-n

-- 
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
http://vorpus.org
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to