display numpy array as image
Hello all, I'm curious if people have experience with / preferences for how to display a numpy array onscreen as an image. Pyglet looks relatively easy -- you can feed an image buffer object with a string or a ctypes pointer. I presume getting a string from an array is plenty fast, but the ctypes pointer option is intriguing as it allows for dealing with simple strided arrays (the image objects allow for an arbitrary number of bytes between rows). Is it possible to get a ctypes pointer to the beginning of the array buffer from numpy without too much ugliness? wxPython looks pretty easy too, as there are facilities for getting pixels from a buffer. Does anyone have any experience with these? Are there ways of allowing a numpy array and a wxPython image to point to the same memory? Anyhow, these are specific questions, but I'd also appreciate any general thoughts about good approaches for getting pixels from numpy arrays onscreen. Thanks, Zach
I am not sure I got what you mean but I am using PIL to convert arrays to images and viceversa see http://mail.python.org/pipermail/image-sig/2006-September/004099.html I embed bmps using wxpython. Zachary Pincus wrote:
Hello all,
I'm curious if people have experience with / preferences for how to display a numpy array onscreen as an image.
Pyglet looks relatively easy -- you can feed an image buffer object with a string or a ctypes pointer. I presume getting a string from an array is plenty fast, but the ctypes pointer option is intriguing as it allows for dealing with simple strided arrays (the image objects allow for an arbitrary number of bytes between rows). Is it possible to get a ctypes pointer to the beginning of the array buffer from numpy without too much ugliness?
wxPython looks pretty easy too, as there are facilities for getting pixels from a buffer. Does anyone have any experience with these? Are there ways of allowing a numpy array and a wxPython image to point to the same memory?
Anyhow, these are specific questions, but I'd also appreciate any general thoughts about good approaches for getting pixels from numpy arrays onscreen.
Thanks,
Zach _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
-- giorgio@gilestro.tk http://www.cafelamarck.it
On 30/11/2007, Zachary Pincus <zpincus@stanford.edu> wrote:
Hello all,
I'm curious if people have experience with / preferences for how to display a numpy array onscreen as an image.
I'm not sure if you're after anything specific, but a very convenient way to show 2-D arrays on screen is matplotlib (mpl), which is the recommended graphical interface to numpy. With the pylab interface to mpl, the command is as simple as
pylab.imshow(arr) or pylab.matshow(arr) for slightly different axis behaviour.
Angus. -- AJC McMorland, PhD Student Physiology, University of Auckland
On Nov 29, 2007 2:32 PM, Zachary Pincus <zpincus@stanford.edu> wrote:
Hello all,
I'm curious if people have experience with / preferences for how to display a numpy array onscreen as an image.
Pyglet looks relatively easy -- you can feed an image buffer object with a string or a ctypes pointer. I presume getting a string from an array is plenty fast, but the ctypes pointer option is intriguing as it allows for dealing with simple strided arrays (the image objects allow for an arbitrary number of bytes between rows). Is it possible to get a ctypes pointer to the beginning of the array buffer from numpy without too much ugliness?
wxPython looks pretty easy too, as there are facilities for getting pixels from a buffer. Does anyone have any experience with these? Are there ways of allowing a numpy array and a wxPython image to point to the same memory?
Anyhow, these are specific questions, but I'd also appreciate any general thoughts about good approaches for getting pixels from numpy arrays onscreen.
Thanks,
Zach _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Have you tried the matplotlib/pylab module? It has pretty simple ways of doing what you're thinking of if I'm understanding your intent correctly.. Josh
Zachary Pincus wrote:
wxPython looks pretty easy too, as there are facilities for getting pixels from a buffer. Does anyone have any experience with these?
some.
Are there ways of allowing a numpy array and a wxPython image to point to the same memory?
yup. You can build a wxImage from a buffer, and numpy provides a buffer interface, so they end up with them sharing the same memory, as long as your numpy array is contiguous 24 rgb. I've enclosed a sample that generates a wx.Image from a numpy array, then every time you push the button, the array is altered in-place, and you can see the image change. ( think you need at least wxPython 2.8 for this to work) -Chris -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
Zachary Pincus wrote:
Hello all,
I'm curious if people have experience with / preferences for how to display a numpy array onscreen as an image.
Pyglet looks relatively easy -- you can feed an image buffer object with a string or a ctypes pointer. I presume getting a string from an array is plenty fast, but the ctypes pointer option is intriguing as it allows for dealing with simple strided arrays (the image objects allow for an arbitrary number of bytes between rows). Is it possible to get a ctypes pointer to the beginning of the array buffer from numpy without too much ugliness?
In [16]: from numpy import * In [17]: a = arange(10) In [18]: dir(a.ctypes) Out[18]: ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', '_arr', '_as_parameter_', '_ctypes', '_data', '_zerod', 'data', 'data_as', 'get_as_parameter', 'get_data', 'get_shape', 'get_strides', 'shape', 'shape_as', 'strides', 'strides_as'] In [22]: import ctypes In [24]: a.ctypes.data_as(ctypes.POINTER(ctypes.c_long)) Out[24]: <ctypes.LP_c_long object at 0x1b353a0> In [25]: a.ctypes.get_shape() Out[25]: <numpy.core._internal.c_long_Array_1 object at 0x1c096c0> In [26]: a.ctypes.get_strides() Out[26]: <numpy.core._internal.c_long_Array_1 object at 0x1c09710> In [27]: a.ctypes.get_as_parameter() Out[27]: c_void_p(27442576) You might want to use the new ctypes-based OpenGL 3.0+ package. It has numpy support a bit more directly. You can use Pyglet for its windowing and all of the other surrounding infrastructure and use OpenGL directly for the drawing. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco
participants (6)
-
Angus McMorland
-
Christopher Barker
-
Giorgio F. Gilestro
-
Joshua Lippai
-
Robert Kern
-
Zachary Pincus