Decoding images with GDK-PixBuf

The ImageSurface.create_from_png() method can decode PNG images and provide a cairo surface, but what about other image formats?

The cairocffi.pixbuf module uses GDK-PixBuf to decode JPEG, GIF, and various other formats (depending on what is installed.) If you don’t import this module, it is possible to use the rest of cairocffi without having GDK-PixBuf installed. GDK-PixBuf is an independent package since version 2.22, but before that was part of GTK+.

This module also converts pixel data since the internal format in GDK-PixBuf (big-endian RGBA) is not the same as in cairo (native-endian ARGB). For this reason, although it is a “toy” API, ImageSurface.create_from_png() can be faster than decode_to_image_surface() if the format is known to be PNG. The pixel conversion is done by GTK+ if available, but a (slower) fallback method is used otherwise.