Skip to content Skip to sidebar Skip to footer

How To Turn Gdk Pixbuf Object Into Numpy Array

So, I am trying to take a screenshot of the screen and do some stuff with it as numpy array. However, all I could find is turning it into PIL Image first and then into numpy array.

Solution 1:

If you don't mind using Gtk2 and PyGtk, then the following will do

import gtk, numpy
image="yourimage.jpg"
t=gtk.gdk.pixbuf_new_from_file(image)
n=t.get_pixels_array()
print n.shape

The best part is that n is a reference to the data in t so you can alter the pixbuf using numpy

If you wish to use Gtk3, these two calls will do

from gi.repository import GdkPixbuf
import numpy
def array_from_pixbuf(p):
    " convert from GdkPixbuf to numpy array"
    w,h,c,r=(p.get_width(), p.get_height(), p.get_n_channels(), p.get_rowstride())
    assert p.get_colorspace() == GdkPixbuf.Colorspace.RGB
    assert p.get_bits_per_sample() == 8
    if  p.get_has_alpha():
        assert c == 4
    else:
        assert c == 3
    assert r >= w * c
    a=numpy.frombuffer(p.get_pixels(),dtype=numpy.uint8)
    if a.shape[0] == w*c*h:
        return a.reshape( (h, w, c) )
    else:
        b=numpy.zeros((h,w*c),'uint8')
        for j in range(h):
            b[j,:]=a[r*j:r*j+w*c]
        return b.reshape( (h, w, c) )

def pixbuf_from_array(z):
    " convert from numpy array to GdkPixbuf "
    z=z.astype('uint8')
    h,w,c=z.shape
    assert c == 3 or c == 4
    if hasattr(GdkPixbuf.Pixbuf,'new_from_bytes'):
        Z = GLib.Bytes.new(z.tobytes())
        return GdkPixbuf.Pixbuf.new_from_bytes(Z, GdkPixbuf.Colorspace.RGB, c==4, 8, w, h, w*c)
    return GdkPixbuf.Pixbuf.new_from_data(z.tobytes(),  GdkPixbuf.Colorspace.RGB, c==4, 8, w, h, w*c, None, None)

Post a Comment for "How To Turn Gdk Pixbuf Object Into Numpy Array"