gdkpixbuf.pixbuf.Pixbuf contains information about an image's pixel data,
its color space, bits per sample, width and height, and the
rowstride (the number of bytes between the start of one row
and the start of the next).
Every time you create a new gdkpixbuf.pixbuf.Pixbuf instance for some data, you
will need to specify the destroy notification function that will be
called when the data buffer needs to be freed; this will happen when
a gdkpixbuf.pixbuf.Pixbuf is finalized by the reference counting functions. If
you have a chunk of static data compiled into your application, you
can pass in NULL as the destroy notification function so that the
data will not be freed.
The gdkpixbuf.pixbuf.Pixbuf.new_ constructor function can be used
as a convenience to create a pixbuf with an empty buffer; this is
equivalent to allocating a data buffer using malloc() and then
wrapping it with [gdkpixbuf.pixbuf.Pixbuf.newFromData]. The [gdkpixbuf.pixbuf.Pixbuf.new_]
function will compute an optimal rowstride so that rendering can be
performed with an efficient algorithm.
You can also copy an existing pixbuf with the gdkpixbuf.pixbuf.Pixbuf.copy
function. This is not the same as just acquiring a reference to
the old pixbuf instance: the copy function will actually duplicate
the pixel data in memory and create a new class@Pixbuf instance
for it.
gdkpixbuf.pixbuf.Pixbuf structures are reference counted. This means that an
application can share a single pixbuf among many parts of the
code. When a piece of the program needs to use a pixbuf, it should
acquire a reference to it by calling [gobject.object.ObjectG.ref_]; when it no
longer needs the pixbuf, it should release the reference it acquired
by calling [gobject.object.ObjectG.unref]. The resources associated with a
gdkpixbuf.pixbuf.Pixbuf will be freed when its reference count drops to zero.
Newly-created gdkpixbuf.pixbuf.Pixbuf instances start with a reference count
of one.
Image data in a pixbuf is stored in memory in an uncompressed,
packed format. Rows in the image are stored top to bottom, and
in each row pixels are stored from left to right.
**NOTE**: If you are copying raw pixbuf data with memcpy() note that the
last row in the pixbuf may not be as wide as the full rowstride, but rather
just as wide as the pixel data needs to be; that is: it is unsafe to do
memcpy (dest, pixels, rowstride * height) to copy a whole pixbuf. Use
gdkpixbuf.pixbuf.Pixbuf.copy instead, or compute the width in bytes of the
last row as:
The gdkpixbuf.pixbuf.Pixbuf class provides methods for saving image data in
a number of file formats. The formatted data can be written to a
file or to a memory buffer. gdkpixbuf.pixbuf.Pixbuf can also call a user-defined
callback on the data, which allows to e.g. write the image
to a socket or store it in a database.
A pixel buffer.
gdkpixbuf.pixbuf.Pixbuf contains information about an image's pixel data, its color space, bits per sample, width and height, and the rowstride (the number of bytes between the start of one row and the start of the next).
Creating new gdkpixbuf.pixbuf.Pixbuf
The most basic way to create a pixbuf is to wrap an existing pixel buffer with a gdkpixbuf.pixbuf.Pixbuf instance. You can use the gdkpixbuf.pixbuf.Pixbuf.newFromData function to do this.
Every time you create a new gdkpixbuf.pixbuf.Pixbuf instance for some data, you will need to specify the destroy notification function that will be called when the data buffer needs to be freed; this will happen when a gdkpixbuf.pixbuf.Pixbuf is finalized by the reference counting functions. If you have a chunk of static data compiled into your application, you can pass in NULL as the destroy notification function so that the data will not be freed.
The gdkpixbuf.pixbuf.Pixbuf.new_ constructor function can be used as a convenience to create a pixbuf with an empty buffer; this is equivalent to allocating a data buffer using malloc() and then wrapping it with [gdkpixbuf.pixbuf.Pixbuf.newFromData]. The [gdkpixbuf.pixbuf.Pixbuf.new_] function will compute an optimal rowstride so that rendering can be performed with an efficient algorithm.
As a special case, you can use the gdkpixbuf.pixbuf.Pixbuf.newFromXpmData function to create a pixbuf from inline XPM image data.
You can also copy an existing pixbuf with the gdkpixbuf.pixbuf.Pixbuf.copy function. This is not the same as just acquiring a reference to the old pixbuf instance: the copy function will actually duplicate the pixel data in memory and create a new class@Pixbuf instance for it.
Reference counting
gdkpixbuf.pixbuf.Pixbuf structures are reference counted. This means that an application can share a single pixbuf among many parts of the code. When a piece of the program needs to use a pixbuf, it should acquire a reference to it by calling [gobject.object.ObjectG.ref_]; when it no longer needs the pixbuf, it should release the reference it acquired by calling [gobject.object.ObjectG.unref]. The resources associated with a gdkpixbuf.pixbuf.Pixbuf will be freed when its reference count drops to zero. Newly-created gdkpixbuf.pixbuf.Pixbuf instances start with a reference count of one.
Image Data
Image data in a pixbuf is stored in memory in an uncompressed, packed format. Rows in the image are stored top to bottom, and in each row pixels are stored from left to right.
There may be padding at the end of a row.
The "rowstride" value of a pixbuf, as returned by gdkpixbuf.pixbuf.Pixbuf.getRowstride, indicates the number of bytes between rows.
**NOTE**: If you are copying raw pixbuf data with memcpy() note that the last row in the pixbuf may not be as wide as the full rowstride, but rather just as wide as the pixel data needs to be; that is: it is unsafe to do memcpy (dest, pixels, rowstride * height) to copy a whole pixbuf. Use gdkpixbuf.pixbuf.Pixbuf.copy instead, or compute the width in bytes of the last row as:
The same rule applies when iterating over each row of a gdkpixbuf.pixbuf.Pixbuf pixels array.
The following code illustrates a simple put_pixel() function for RGB pixbufs with 8 bits per channel with an alpha channel.
Loading images
The GdkPixBuf class provides a simple mechanism for loading an image from a file in synchronous and asynchronous fashion.
For GUI applications, it is recommended to use the asynchronous stream API to avoid blocking the control flow of the application.
Additionally, gdkpixbuf.pixbuf.Pixbuf provides the gdkpixbuf.pixbuf_loader.PixbufLoader API for progressive image loading.
Saving images
The gdkpixbuf.pixbuf.Pixbuf class provides methods for saving image data in a number of file formats. The formatted data can be written to a file or to a memory buffer. gdkpixbuf.pixbuf.Pixbuf can also call a user-defined callback on the data, which allows to e.g. write the image to a socket or store it in a database.