[Numpy-discussion] display numpy array as image

Robert Kern robert.kern@gmail....
Thu Nov 29 17:33:12 CST 2007

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)

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

More information about the Numpy-discussion mailing list