Btw, I just wanted to file a bug at PySide, but it might be alright at their end, because I can do this:
from PySide import QtGui image = QtGui.QImage('/home/peter/code/pyTools/sandbox/images/faceDemo.jpg') a = image.bits() del image a #<read-write buffer ptr 0x7f5fe0034010, size 1478400 at 0x3c1a6b0> On 11/26/2013 09:37 PM, Peter Rennert 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} > > 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> > > del image > > b > # booom # > > On 11/26/2013 08:12 PM, Peter Rennert wrote: >> Brilliant thanks, I will try out the "little class" approach. >> >> On 11/26/2013 08:03 PM, Nathaniel Smith wrote: >>> On Tue, Nov 26, 2013 at 11:54 AM, Peter Rennert >>> <p.renn...@cs.ucl.ac.uk> wrote: >>>> Hi, >>>> >>>> I as the title says, I am looking for a way to set in python the >>>> base of >>>> an ndarray to an object. >>>> >>>> Use case is porting qimage2ndarray to PySide where I want to do >>>> something like: >>>> >>>> In [1]: from PySide import QtGui >>>> >>>> In [2]: image = >>>> QtGui.QImage('/home/peter/code/pyTools/sandbox/images/faceDemo.jpg') >>>> >>>> In [3]: import numpy as np >>>> >>>> In [4]: a = np.frombuffer(image.bits()) >>>> >>>> --> I would like to do something like: >>>> In [5]: a.base = image >>>> >>>> --> to avoid situations such as: >>>> In [6]: del image >>>> >>>> In [7]: a >>>> Segmentation fault (core dumped) >>> This is a bug in PySide -- the buffer object returned by image.bits() >>> needs to hold a reference to the original image. Please report a bug >>> to them. You will also get a segfault from code that doesn't use numpy >>> at all, by doing things like: >>> >>> bits = image.bits() >>> del image >>> <anything involving the bits object> >>> >>> As a workaround, you can write a little class with an >>> __array_interface__ attribute that points to the image's contents, and >>> then call np.asarray() on this object. The resulting array will have >>> your object as its .base, and then your object can hold onto whatever >>> references it wants. >>> >>> -n >>> _______________________________________________ >>> NumPy-Discussion mailing list >>> NumPy-Discussion@scipy.org >>> http://mail.scipy.org/mailman/listinfo/numpy-discussion >> _______________________________________________ >> NumPy-Discussion mailing list >> NumPy-Discussion@scipy.org >> http://mail.scipy.org/mailman/listinfo/numpy-discussion > _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion