Pixbuf

class Pixbuf(**properties: Any)

Superclasses: Object

Implemented Interfaces: Icon, LoadableIcon

A pixel buffer.

GdkPixbuf 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

The most basic way to create a pixbuf is to wrap an existing pixel buffer with a Pixbuf instance. You can use the [ctor``GdkPixbuf`.Pixbuf.new_from_data`] function to do this.

Every time you create a new GdkPixbuf 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 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 [ctor``GdkPixbuf`.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 :func:`~gi.repository.GdkPixbuf.Pixbuf.new_from_data`. The :func:`~gi.repository.GdkPixbuf.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 [ctor``GdkPixbuf`.Pixbuf.new_from_xpm_data`] function to create a pixbuf from inline XPM image data.

You can also copy an existing pixbuf with the 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 Pixbuf instance for it.

Reference counting

GdkPixbuf 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 :func:`~gi.repository.GObject.GObject.Object.ref`; when it no longer needs the pixbuf, it should release the reference it acquired by calling :func:`~gi.repository.GObject.GObject.Object.unref`. The resources associated with a GdkPixbuf will be freed when its reference count drops to zero. Newly-created GdkPixbuf 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 [method``GdkPixbuf`.Pixbuf.get_rowstride`], 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 copy instead, or compute the width in bytes of the last row as:

last_row = width * ((n_channels * bits_per_sample + 7) / 8);

The same rule applies when iterating over each row of a GdkPixbuf pixels array.

The following code illustrates a simple put_pixel() function for RGB pixbufs with 8 bits per channel with an alpha channel.

static void
put_pixel (GdkPixbuf *pixbuf,
           int x,
           int y,
           guchar red,
           guchar green,
           guchar blue,
           guchar alpha)
{
  int n_channels = gdk_pixbuf_get_n_channels (pixbuf);

  // Ensure that the pixbuf is valid
  g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
  g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
  g_assert (gdk_pixbuf_get_has_alpha (pixbuf));
  g_assert (n_channels == 4);

  int width = gdk_pixbuf_get_width (pixbuf);
  int height = gdk_pixbuf_get_height (pixbuf);

  // Ensure that the coordinates are in a valid range
  g_assert (x >= 0 && x < width);
  g_assert (y >= 0 && y < height);

  int rowstride = gdk_pixbuf_get_rowstride (pixbuf);

  // The pixel buffer in the GdkPixbuf instance
  guchar *pixels = gdk_pixbuf_get_pixels (pixbuf);

  // The pixel we wish to modify
  guchar *p = pixels + y * rowstride + x * n_channels;
  p[0] = red;
  p[1] = green;
  p[2] = blue;
  p[3] = alpha;
}

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 provides the PixbufLoader` API for progressive image loading.

Saving images

The GdkPixbuf 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 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.

Constructors

class Pixbuf
classmethod new(colorspace: Colorspace, has_alpha: bool, bits_per_sample: int, width: int, height: int) Pixbuf | None

Creates a new GdkPixbuf structure and allocates a buffer for it.

If the allocation of the buffer failed, this function will return NULL.

The buffer has an optimal rowstride. Note that the buffer is not cleared; you will have to fill it completely yourself.

Parameters:
  • colorspace – Color space for image

  • has_alpha – Whether the image should have transparency information

  • bits_per_sample – Number of bits per color sample

  • width – Width of image in pixels, must be > 0

  • height – Height of image in pixels, must be > 0

classmethod new_from_bytes(data: Bytes, colorspace: Colorspace, has_alpha: bool, bits_per_sample: int, width: int, height: int, rowstride: int) Pixbuf

Creates a new Pixbuf out of in-memory readonly image data.

Currently only RGB images with 8 bits per sample are supported.

This is the GBytes variant of new_from_data(), useful for language bindings.

Added in version 2.32.

Parameters:
  • data – Image data in 8-bit/sample packed format inside a Bytes

  • colorspace – Colorspace for the image data

  • has_alpha – Whether the data has an opacity channel

  • bits_per_sample – Number of bits per sample

  • width – Width of the image in pixels, must be > 0

  • height – Height of the image in pixels, must be > 0

  • rowstride – Distance in bytes between row starts

classmethod new_from_file(filename: str) Pixbuf | None

Creates a new pixbuf by loading an image from a file.

The file format is detected automatically.

If NULL is returned, then error will be set. Possible errors are:

  • the file could not be opened

  • there is no loader for the file’s format

  • there is not enough memory to allocate the image buffer

  • the image buffer contains invalid data

The error domains are GDK_PIXBUF_ERROR and G_FILE_ERROR.

Parameters:

filename – Name of file to load, in the GLib file name encoding

classmethod new_from_file_at_scale(filename: str, width: int, height: int, preserve_aspect_ratio: bool) Pixbuf | None

Creates a new pixbuf by loading an image from a file.

The file format is detected automatically.

If NULL is returned, then error will be set. Possible errors are:

  • the file could not be opened

  • there is no loader for the file’s format

  • there is not enough memory to allocate the image buffer

  • the image buffer contains invalid data

The error domains are GDK_PIXBUF_ERROR and G_FILE_ERROR.

The image will be scaled to fit in the requested size, optionally preserving the image’s aspect ratio.

When preserving the aspect ratio, a width of -1 will cause the image to be scaled to the exact given height, and a height of -1 will cause the image to be scaled to the exact given width. When not preserving aspect ratio, a width or height of -1 means to not scale the image at all in that dimension. Negative values for width and height are allowed since 2.8.

Added in version 2.6.

Parameters:
  • filename – Name of file to load, in the GLib file name encoding

  • width – The width the image should have or -1 to not constrain the width

  • height – The height the image should have or -1 to not constrain the height

  • preserve_aspect_ratioTRUE to preserve the image’s aspect ratio

classmethod new_from_file_at_size(filename: str, width: int, height: int) Pixbuf | None

Creates a new pixbuf by loading an image from a file.

The file format is detected automatically.

If NULL is returned, then error will be set. Possible errors are:

  • the file could not be opened

  • there is no loader for the file’s format

  • there is not enough memory to allocate the image buffer

  • the image buffer contains invalid data

The error domains are GDK_PIXBUF_ERROR and G_FILE_ERROR.

The image will be scaled to fit in the requested size, preserving the image’s aspect ratio. Note that the returned pixbuf may be smaller than width x height, if the aspect ratio requires it. To load and image at the requested size, regardless of aspect ratio, use new_from_file_at_scale.

Added in version 2.4.

Parameters:
  • filename – Name of file to load, in the GLib file name encoding

  • width – The width the image should have or -1 to not constrain the width

  • height – The height the image should have or -1 to not constrain the height

classmethod new_from_inline(data: Sequence[int], copy_pixels: bool) Pixbuf

Creates a GdkPixbuf from a flat representation that is suitable for storing as inline data in a program.

This is useful if you want to ship a program with images, but don’t want to depend on any external files.

GdkPixbuf ships with a program called gdk-pixbuf-csource, which allows for conversion of GdkPixbuf’s into such a inline representation.

In almost all cases, you should pass the --raw option to gdk-pixbuf-csource. A sample invocation would be:

gdk-pixbuf-csource --raw --name=myimage_inline myimage.png

For the typical case where the inline pixbuf is read-only static data, you don’t need to copy the pixel data unless you intend to write to it, so you can pass FALSE for copy_pixels. If you pass --rle to gdk-pixbuf-csource, a copy will be made even if copy_pixels is FALSE, so using this option is generally a bad idea.

If you create a pixbuf from const inline data compiled into your program, it’s probably safe to ignore errors and disable length checks, since things will always succeed:

pixbuf = gdk_pixbuf_new_from_inline (-1, myimage_inline, FALSE, NULL);

For non-const inline data, you could get out of memory. For untrusted inline data located at runtime, you could have corrupt inline data in addition.

Deprecated since version 2.32: Use GResource instead.

Parameters:
  • data – Byte data containing a serialized GdkPixdata structure

  • copy_pixels – Whether to copy the pixel data, or use direct pointers data for the resulting pixbuf

classmethod new_from_resource(resource_path: str) Pixbuf | None

Creates a new pixbuf by loading an image from an resource.

The file format is detected automatically. If NULL is returned, then error will be set.

Added in version 2.26.

Parameters:

resource_path – the path of the resource file

classmethod new_from_resource_at_scale(resource_path: str, width: int, height: int, preserve_aspect_ratio: bool) Pixbuf | None

Creates a new pixbuf by loading an image from an resource.

The file format is detected automatically. If NULL is returned, then error will be set.

The image will be scaled to fit in the requested size, optionally preserving the image’s aspect ratio. When preserving the aspect ratio, a width of -1 will cause the image to be scaled to the exact given height, and a height of -1 will cause the image to be scaled to the exact given width. When not preserving aspect ratio, a width or height of -1 means to not scale the image at all in that dimension.

The stream is not closed.

Added in version 2.26.

Parameters:
  • resource_path – the path of the resource file

  • width – The width the image should have or -1 to not constrain the width

  • height – The height the image should have or -1 to not constrain the height

  • preserve_aspect_ratioTRUE to preserve the image’s aspect ratio

classmethod new_from_stream(stream: InputStream, cancellable: Cancellable | None = None) Pixbuf | None

Creates a new pixbuf by loading an image from an input stream.

The file format is detected automatically.

If NULL is returned, then error will be set.

The cancellable can be used to abort the operation from another thread. If the operation was cancelled, the error G_IO_ERROR_CANCELLED will be returned. Other possible errors are in the GDK_PIXBUF_ERROR and G_IO_ERROR domains.

The stream is not closed.

Added in version 2.14.

Parameters:
  • stream – a GInputStream to load the pixbuf from

  • cancellable – optional GCancellable object, NULL to ignore

classmethod new_from_stream_at_scale(stream: InputStream, width: int, height: int, preserve_aspect_ratio: bool, cancellable: Cancellable | None = None) Pixbuf | None

Creates a new pixbuf by loading an image from an input stream.

The file format is detected automatically. If NULL is returned, then error will be set. The cancellable can be used to abort the operation from another thread. If the operation was cancelled, the error G_IO_ERROR_CANCELLED will be returned. Other possible errors are in the GDK_PIXBUF_ERROR and G_IO_ERROR domains.

The image will be scaled to fit in the requested size, optionally preserving the image’s aspect ratio.

When preserving the aspect ratio, a width of -1 will cause the image to be scaled to the exact given height, and a height of -1 will cause the image to be scaled to the exact given width. If both width and height are given, this function will behave as if the smaller of the two values is passed as -1.

When not preserving aspect ratio, a width or height of -1 means to not scale the image at all in that dimension.

The stream is not closed.

Added in version 2.14.

Parameters:
  • stream – a GInputStream to load the pixbuf from

  • width – The width the image should have or -1 to not constrain the width

  • height – The height the image should have or -1 to not constrain the height

  • preserve_aspect_ratioTRUE to preserve the image’s aspect ratio

  • cancellable – optional GCancellable object, NULL to ignore

classmethod new_from_stream_finish(async_result: AsyncResult) Pixbuf | None

Finishes an asynchronous pixbuf creation operation started with new_from_stream_async().

Added in version 2.24.

Parameters:

async_result – a GAsyncResult

classmethod new_from_xpm_data(data: Sequence[str]) Pixbuf

Creates a new pixbuf by parsing XPM data in memory.

This data is commonly the result of including an XPM file into a program’s C source.

Parameters:

data – Pointer to inline XPM data.

Methods

class Pixbuf
add_alpha(substitute_color: bool, r: int, g: int, b: int) Pixbuf

Takes an existing pixbuf and adds an alpha channel to it.

If the existing pixbuf already had an alpha channel, the channel values are copied from the original; otherwise, the alpha channel is initialized to 255 (full opacity).

If substitute_color is TRUE, then the color specified by the (r, g, b) arguments will be assigned zero opacity. That is, if you pass (255, 255, 255) for the substitute color, all white pixels will become fully transparent.

If substitute_color is FALSE, then the (r, g, b) arguments will be ignored.

Parameters:
  • substitute_color – Whether to set a color to zero opacity.

  • r – Red value to substitute.

  • g – Green value to substitute.

  • b – Blue value to substitute.

apply_embedded_orientation() Pixbuf | None

Takes an existing pixbuf and checks for the presence of an associated “orientation” option.

The orientation option may be provided by the JPEG loader (which reads the exif orientation tag) or the TIFF loader (which reads the TIFF orientation tag, and compensates it for the partial transforms performed by libtiff).

If an orientation option/tag is present, the appropriate transform will be performed so that the pixbuf is oriented correctly.

Added in version 2.12.

calculate_rowstride(colorspace: Colorspace, has_alpha: bool, bits_per_sample: int, width: int, height: int) int

Calculates the rowstride that an image created with those values would have.

This function is useful for front-ends and backends that want to check image values without needing to create a GdkPixbuf.

Added in version 2.36.8.

Parameters:
  • colorspace – Color space for image

  • has_alpha – Whether the image should have transparency information

  • bits_per_sample – Number of bits per color sample

  • width – Width of image in pixels, must be > 0

  • height – Height of image in pixels, must be > 0

composite(dest: Pixbuf, dest_x: int, dest_y: int, dest_width: int, dest_height: int, offset_x: float, offset_y: float, scale_x: float, scale_y: float, interp_type: InterpType, overall_alpha: int) None

Creates a transformation of the source image src by scaling by scale_x and scale_y then translating by offset_x and offset_y.

This gives an image in the coordinates of the destination pixbuf. The rectangle (dest_x, dest_y, dest_width, dest_height) is then alpha blended onto the corresponding rectangle of the original destination image.

When the destination rectangle contains parts not in the source image, the data at the edges of the source image is replicated to infinity.

https://docs.gtk.org/gdk-pixbuf/composite.png
Parameters:
  • dest – the Pixbuf into which to render the results

  • dest_x – the left coordinate for region to render

  • dest_y – the top coordinate for region to render

  • dest_width – the width of the region to render

  • dest_height – the height of the region to render

  • offset_x – the offset in the X direction (currently rounded to an integer)

  • offset_y – the offset in the Y direction (currently rounded to an integer)

  • scale_x – the scale factor in the X direction

  • scale_y – the scale factor in the Y direction

  • interp_type – the interpolation type for the transformation.

  • overall_alpha – overall alpha for source image (0..255)

composite_color(dest: Pixbuf, dest_x: int, dest_y: int, dest_width: int, dest_height: int, offset_x: float, offset_y: float, scale_x: float, scale_y: float, interp_type: InterpType, overall_alpha: int, check_x: int, check_y: int, check_size: int, color1: int, color2: int) None

Creates a transformation of the source image src by scaling by scale_x and scale_y then translating by offset_x and offset_y, then alpha blends the rectangle (dest_x ,``dest_y``, dest_width, dest_height) of the resulting image with a checkboard of the colors color1 and color2 and renders it onto the destination image.

If the source image has no alpha channel, and overall_alpha is 255, a fast path is used which omits the alpha blending and just performs the scaling.

See composite_color_simple() for a simpler variant of this function suitable for many tasks.

Parameters:
  • dest – the Pixbuf into which to render the results

  • dest_x – the left coordinate for region to render

  • dest_y – the top coordinate for region to render

  • dest_width – the width of the region to render

  • dest_height – the height of the region to render

  • offset_x – the offset in the X direction (currently rounded to an integer)

  • offset_y – the offset in the Y direction (currently rounded to an integer)

  • scale_x – the scale factor in the X direction

  • scale_y – the scale factor in the Y direction

  • interp_type – the interpolation type for the transformation.

  • overall_alpha – overall alpha for source image (0..255)

  • check_x – the X offset for the checkboard (origin of checkboard is at -check_x, -check_y)

  • check_y – the Y offset for the checkboard

  • check_size – the size of checks in the checkboard (must be a power of two)

  • color1 – the color of check at upper left

  • color2 – the color of the other check

composite_color_simple(dest_width: int, dest_height: int, interp_type: InterpType, overall_alpha: int, check_size: int, color1: int, color2: int) Pixbuf | None

Creates a new pixbuf by scaling src to dest_width x dest_height and alpha blending the result with a checkboard of colors color1 and color2.

Parameters:
  • dest_width – the width of destination image

  • dest_height – the height of destination image

  • interp_type – the interpolation type for the transformation.

  • overall_alpha – overall alpha for source image (0..255)

  • check_size – the size of checks in the checkboard (must be a power of two)

  • color1 – the color of check at upper left

  • color2 – the color of the other check

fill(pixel: int) None

Clears a pixbuf to the given RGBA value, converting the RGBA value into the pixbuf’s pixel format.

The alpha component will be ignored if the pixbuf doesn’t have an alpha channel.

Parameters:

pixel – RGBA pixel to used to clear (0xffffffff is opaque white, 0x00000000 transparent black)

flip(horizontal: bool) Pixbuf | None

Flips a pixbuf horizontally or vertically and returns the result in a new pixbuf.

Added in version 2.6.

Parameters:

horizontalTRUE to flip horizontally, FALSE to flip vertically

get_bits_per_sample() int

Queries the number of bits per color sample in a pixbuf.

get_byte_length() int

Returns the length of the pixel data, in bytes.

Added in version 2.26.

get_colorspace() Colorspace

Queries the color space of a pixbuf.

get_file_info(filename: str) tuple[PixbufFormat | None, int, int]

Parses an image file far enough to determine its format and size.

Added in version 2.4.

Parameters:

filename – The name of the file to identify.

get_file_info_async(filename: str, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

Asynchronously parses an image file far enough to determine its format and size.

For more details see get_file_info(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread. You can then call get_file_info_finish() to get the result of the operation.

Added in version 2.32.

Parameters:
  • filename – The name of the file to identify

  • cancellable – optional GCancellable object, NULL to ignore

  • callback – a GAsyncReadyCallback to call when the file info is available

  • user_data – the data to pass to the callback function

get_file_info_finish(async_result: AsyncResult) tuple[PixbufFormat | None, int, int]

Finishes an asynchronous pixbuf parsing operation started with get_file_info_async().

Added in version 2.32.

Parameters:

async_result – a GAsyncResult

get_formats() list[PixbufFormat]

Obtains the available information about the image formats supported by GdkPixbuf.

Added in version 2.2.

get_has_alpha() bool

Queries whether a pixbuf has an alpha channel (opacity information).

get_height() int

Queries the height of a pixbuf.

get_n_channels() int

Queries the number of channels of a pixbuf.

get_option(key: str) str | None

Looks up key in the list of options that may have been attached to the pixbuf when it was loaded, or that may have been attached by another function using set_option().

For instance, the ANI loader provides “Title” and “Artist” options. The ICO, XBM, and XPM loaders provide “x_hot” and “y_hot” hot-spot options for cursor definitions. The PNG loader provides the tEXt ancillary chunk key/value pairs as options. Since 2.12, the TIFF and JPEG loaders return an “orientation” option string that corresponds to the embedded TIFF/Exif orientation tag (if present). Since 2.32, the TIFF loader sets the “multipage” option string to “yes” when a multi-page TIFF is loaded. Since 2.32 the JPEG and PNG loaders set “x-dpi” and “y-dpi” if the file contains image density information in dots per inch. Since 2.36.6, the JPEG loader sets the “comment” option with the comment EXIF tag.

Parameters:

key – a nul-terminated string.

get_options() dict[str, str]

Returns a GHashTable with a list of all the options that may have been attached to the pixbuf when it was loaded, or that may have been attached by another function using set_option.

Added in version 2.32.

get_pixels() bytes

Queries a pointer to the pixel data of a pixbuf.

This function will cause an implicit copy of the pixbuf data if the pixbuf was created from read-only data.

Please see the section on image data for information about how the pixel data is stored in memory.

get_rowstride() int

Queries the rowstride of a pixbuf, which is the number of bytes between the start of a row and the start of the next row.

get_width() int

Queries the width of a pixbuf.

init_modules(path: str) bool

Initalizes the gdk-pixbuf loader modules referenced by the loaders.cache file present inside that directory.

This is to be used by applications that want to ship certain loaders in a different location from the system ones.

This is needed when the OS or runtime ships a minimal number of loaders so as to reduce the potential attack surface of carefully crafted image files, especially for uncommon file types. Applications that require broader image file types coverage, such as image viewers, would be expected to ship the gdk-pixbuf modules in a separate location, bundled with the application in a separate directory from the OS or runtime- provided modules.

Added in version 2.40.

Parameters:

path – Path to directory where the loaders.cache is installed

classmethod new_from_data(data, colorspace, has_alpha, bits_per_sample, width, height, rowstride, destroy_fn=None, *destroy_fn_data)
Parameters:
  • data

  • colorspace

  • has_alpha

  • bits_per_sample

  • width

  • height

  • rowstride

  • destroy_fn

  • destroy_fn_data

new_from_stream_async(stream: InputStream, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

Creates a new pixbuf by asynchronously loading an image from an input stream.

For more details see new_from_stream(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread. You can then call new_from_stream_finish() to get the result of the operation.

Added in version 2.24.

Parameters:
  • stream – a GInputStream from which to load the pixbuf

  • cancellable – optional GCancellable object, NULL to ignore

  • callback – a GAsyncReadyCallback to call when the pixbuf is loaded

  • user_data – the data to pass to the callback function

new_from_stream_at_scale_async(stream: InputStream, width: int, height: int, preserve_aspect_ratio: bool, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

Creates a new pixbuf by asynchronously loading an image from an input stream.

For more details see new_from_stream_at_scale(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread. You can then call new_from_stream_finish() to get the result of the operation.

Added in version 2.24.

Parameters:
  • stream – a GInputStream from which to load the pixbuf

  • width – the width the image should have or -1 to not constrain the width

  • height – the height the image should have or -1 to not constrain the height

  • preserve_aspect_ratioTRUE to preserve the image’s aspect ratio

  • cancellable – optional GCancellable object, NULL to ignore

  • callback – a GAsyncReadyCallback to call when the pixbuf is loaded

  • user_data – the data to pass to the callback function

new_subpixbuf(src_x: int, src_y: int, width: int, height: int) Pixbuf

Creates a new pixbuf which represents a sub-region of src_pixbuf.

The new pixbuf shares its pixels with the original pixbuf, so writing to one affects both. The new pixbuf holds a reference to src_pixbuf, so src_pixbuf will not be finalized until the new pixbuf is finalized.

Note that if src_pixbuf is read-only, this function will force it to be mutable.

Parameters:
  • src_x – X coord in src_pixbuf

  • src_y – Y coord in src_pixbuf

  • width – width of region in src_pixbuf

  • height – height of region in src_pixbuf

read_pixel_bytes() Bytes

Provides a Bytes buffer containing the raw pixel data; the data must not be modified.

This function allows skipping the implicit copy that must be made if get_pixels() is called on a read-only pixbuf.

Added in version 2.32.

read_pixels() int

Provides a read-only pointer to the raw pixel data.

This function allows skipping the implicit copy that must be made if get_pixels() is called on a read-only pixbuf.

Added in version 2.32.

remove_option(key: str) bool

Removes the key/value pair option attached to a GdkPixbuf.

Added in version 2.36.

Parameters:

key – a nul-terminated string representing the key to remove.

rotate_simple(angle: PixbufRotation) Pixbuf | None

Rotates a pixbuf by a multiple of 90 degrees, and returns the result in a new pixbuf.

If angle is 0, this function will return a copy of src.

Added in version 2.6.

Parameters:

angle – the angle to rotate by

saturate_and_pixelate(dest: Pixbuf, saturation: float, pixelate: bool) None

Modifies saturation and optionally pixelates src, placing the result in dest.

The src and dest pixbufs must have the same image format, size, and rowstride.

The src and dest arguments may be the same pixbuf with no ill effects.

If saturation is 1.0 then saturation is not changed. If it’s less than 1.0, saturation is reduced (the image turns toward grayscale); if greater than 1.0, saturation is increased (the image gets more vivid colors).

If pixelate is TRUE, then pixels are faded in a checkerboard pattern to create a pixelated image.

Parameters:
  • dest – place to write modified version of src

  • saturation – saturation factor

  • pixelate – whether to pixelate

save_to_bufferv(type: str, option_keys: Sequence[str] | None = None, option_values: Sequence[str] | None = None) tuple[bool, bytes]

Vector version of :func:`~gi.repository.GdkPixbuf.Pixbuf.save_to_buffer`.

Saves pixbuf to a new buffer in format type, which is currently “jpeg”, “tiff”, “png”, “ico” or “bmp”.

See save_to_buffer for more details.

Added in version 2.4.

Parameters:
  • type – name of file format.

  • option_keys – name of options to set

  • option_values – values for named options

save_to_callbackv(save_func: Callable[[...], tuple[bool, GError]], type: str, option_keys: Sequence[str] | None = None, option_values: Sequence[str] | None = None, *user_data: Any) bool

Vector version of :func:`~gi.repository.GdkPixbuf.Pixbuf.save_to_callback`.

Saves pixbuf to a callback in format type, which is currently “jpeg”, “png”, “tiff”, “ico” or “bmp”.

If error is set, FALSE will be returned.

See save_to_callback for more details.

Added in version 2.4.

Parameters:
  • save_func – a function that is called to save each block of data that the save routine generates.

  • type – name of file format.

  • option_keys – name of options to set

  • option_values – values for named options

  • user_data – user data to pass to the save function.

save_to_stream_finish(async_result: AsyncResult) bool

Finishes an asynchronous pixbuf save operation started with save_to_stream_async().

Added in version 2.24.

Parameters:

async_result – a GAsyncResult

save_to_streamv(stream: OutputStream, type: str, option_keys: Sequence[str] | None = None, option_values: Sequence[str] | None = None, cancellable: Cancellable | None = None) bool

Saves pixbuf to an output stream.

Supported file formats are currently “jpeg”, “tiff”, “png”, “ico” or “bmp”.

See save_to_stream for more details.

Added in version 2.36.

Parameters:
  • stream – a GOutputStream to save the pixbuf to

  • type – name of file format

  • option_keys – name of options to set

  • option_values – values for named options

  • cancellable – optional GCancellable object, NULL to ignore

save_to_streamv_async(stream: OutputStream, type: str, option_keys: Sequence[str] | None = None, option_values: Sequence[str] | None = None, cancellable: Cancellable | None = None, callback: Callable[[...], None] | None = None, *user_data: Any) None

Saves pixbuf to an output stream asynchronously.

For more details see save_to_streamv(), which is the synchronous version of this function.

When the operation is finished, callback will be called in the main thread.

You can then call save_to_stream_finish() to get the result of the operation.

Added in version 2.36.

Parameters:
  • stream – a GOutputStream to which to save the pixbuf

  • type – name of file format

  • option_keys – name of options to set

  • option_values – values for named options

  • cancellable – optional GCancellable object, NULL to ignore

  • callback – a GAsyncReadyCallback to call when the pixbuf is saved

  • user_data – the data to pass to the callback function

savev(filename: str, type: str, option_keys: Sequence[str] | None = None, option_values: Sequence[str] | None = None) bool

Vector version of :func:`~gi.repository.GdkPixbuf.Pixbuf.save`.

Saves pixbuf to a file in type, which is currently “jpeg”, “png”, “tiff”, “ico” or “bmp”.

If error is set, FALSE will be returned.

See save for more details.

Parameters:
  • filename – name of file to save.

  • type – name of file format.

  • option_keys – name of options to set

  • option_values – values for named options

scale(dest: Pixbuf, dest_x: int, dest_y: int, dest_width: int, dest_height: int, offset_x: float, offset_y: float, scale_x: float, scale_y: float, interp_type: InterpType) None

Creates a transformation of the source image src by scaling by scale_x and scale_y then translating by offset_x and offset_y, then renders the rectangle (dest_x, dest_y, dest_width, dest_height) of the resulting image onto the destination image replacing the previous contents.

Try to use scale_simple() first; this function is the industrial-strength power tool you can fall back to, if scale_simple() isn’t powerful enough.

If the source rectangle overlaps the destination rectangle on the same pixbuf, it will be overwritten during the scaling which results in rendering artifacts.

Parameters:
  • dest – the Pixbuf into which to render the results

  • dest_x – the left coordinate for region to render

  • dest_y – the top coordinate for region to render

  • dest_width – the width of the region to render

  • dest_height – the height of the region to render

  • offset_x – the offset in the X direction (currently rounded to an integer)

  • offset_y – the offset in the Y direction (currently rounded to an integer)

  • scale_x – the scale factor in the X direction

  • scale_y – the scale factor in the Y direction

  • interp_type – the interpolation type for the transformation.

scale_simple(dest_width: int, dest_height: int, interp_type: InterpType) Pixbuf | None

Create a new pixbuf containing a copy of src scaled to dest_width x dest_height.

This function leaves src unaffected.

The interp_type should be GDK_INTERP_NEAREST if you want maximum speed (but when scaling down GDK_INTERP_NEAREST is usually unusably ugly). The default interp_type should be GDK_INTERP_BILINEAR which offers reasonable quality and speed.

You can scale a sub-portion of src by creating a sub-pixbuf pointing into src; see new_subpixbuf.

If dest_width and dest_height are equal to the width and height of src, this function will return an unscaled copy of src.

For more complicated scaling/alpha blending see scale and composite.

Parameters:
  • dest_width – the width of destination image

  • dest_height – the height of destination image

  • interp_type – the interpolation type for the transformation.

set_option(key: str, value: str) bool

Attaches a key/value pair as an option to a GdkPixbuf.

If key already exists in the list of options attached to the pixbuf, the new value is ignored and FALSE is returned.

Added in version 2.2.

Parameters:
  • key – a nul-terminated string.

  • value – a nul-terminated string.

Properties

class Pixbuf
props.bits_per_sample: int

The number of bits per sample.

Currently only 8 bit per sample are supported.

props.colorspace: Colorspace

The color space of the pixbuf.

Currently, only GDK_COLORSPACE_RGB is supported.

props.has_alpha: bool

Whether the pixbuf has an alpha channel.

props.height: int

The number of rows of the pixbuf.

props.n_channels: int

The number of samples per pixel.

Currently, only 3 or 4 samples per pixel are supported.

props.pixel_bytes: Bytes
props.pixels: None

A pointer to the pixel data of the pixbuf.

props.rowstride: int

The number of bytes between the start of a row and the start of the next row.

This number must (obviously) be at least as large as the width of the pixbuf.

props.width: int

The number of columns of the pixbuf.