GstBuffer to numpy conversion

Peter Rennert p.rennert at cs.ucl.ac.uk
Mon Dec 17 11:52:03 PST 2012


I am using gstreamer 0.10 with python bindings.

I want to convert a gst buffer into a numpy array. Online I found only 
examples [1] for audio and they are using

import pygst
pygst.require('0.10')
import gst
import gobject
import numpy as np

def getBuffer(buf):
     array = np.fromBuffer(buf.data, dtype=float32)
     return array

Which is clearly not correct for video frames. Normally I would expect a 
datatype like uchar. In my C++ code I used guint8 which I got from some 
reference online.

However, if I try in python dtype=np.uint8 or dtype="I" (for c-uint8) or 
"B" (for c-uchar) [2], I get some data,  but I am not getting a proper 
length of my array. I am expecting a frame of 1920x1080x3 = 6220800 
pixels. My  buffers are too short. Using "B", I get only an array of 
length 3110400, which is strangely half of what I would expect 
(1920x1080x1.5 (!)).

Has anyone done a conversion from gst-buffers to python? Or an idea what 
I could try?

Thanks

[1] 
http://timeside.googlecode.com/git-history/994b745dd577b2a0474492c56349e1edefaa3aa5/tests/api/gstreamer.py
[2] 
http://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars-built-in


More information about the gstreamer-devel mailing list