[vtkusers] vtkUnsignedCharArray from python

David Gobbi david.gobbi at gmail.com
Thu Dec 30 18:47:18 EST 2010


Hi Kyle,

The VTK data arrays support the python "buffer" protocol, which can be
used to get raw access to the array.  If you have numpy, you can do
the following:

a = numpy.fromstring(image.GetPointData().GetScalars(), 'uint8')

Also, in python 2.6, you can get "raw" access to the data like this:

b = buffer(image.GetPointData().GetScalars())

You can use str(b) to convert the buffer to a string, but for most
purposes you should be able to use the buffer directly without
conversion to a string.

  David


On Thu, Dec 30, 2010 at 9:34 PM, Kyle Cronan <kyle at cronangraf.com> wrote:
> Hi all,
>
> The python wrapper doesn't seem to give any way to convert a
> vtkUnsignedCharArray (such as returned by an image output object that
> is set to output into memory) into a python string.  Am I right about
> that?
>
> It would maybe work if there was something like ExportToVoidPointer
> that did the memory allocation for you.  I took a look at the python
> wrapper generator but it was beyond me how that all works.  Is there
> an easy fix?  In the mean time I will write to a temp file and just
> read that back in.
>
> Thanks,
> Kyle
> --
> Cronan Graf Corporation
> 2215 S. St. Louis Ave
> Chicago, IL  60623
> (312) 504-3279
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>



More information about the vtkusers mailing list