Output Formats

Learn about the different output formats in Pycairo.

Pycairo is capable of outputting images in PNG, PDF, SVG, PS, and EPS formats. However, it is not capable of controlling all the features of each format.

For example, the PNG image format supports various color spaces, compression schemes, and metadata.But, if we generate a PNG image using Pycairo, we can only choose from a limited number of color spaces. We cannot control other aspects of the file. Similarly, the PDF format supports advanced features such as digital signing, editable fields, and color management, which are not available via Pycairo. All the formats come with their own limitations.

We can export an image in a form that is compliant with the supported formats and can be read in by a program that correctly supports any of those formats. However, if we want detailed control over the output parameters, we will need to look at alternative libraries.

ImageSurface

ImageSurface is the type of surface that was used for the course up until now. It creates output that is targeted at pixel data.

An ImageSurface is often used like this:

surface = cairo.ImageSurface(cairo.FORMAT_RGB24, 600, 400)

ctx = cairo.Context(surface)

// Do some drawing with the context

surface.write_to_png('image.png')

We create the blank surface, obtain a Context, draw on the surface via the context, and then write the image out as a PNG file.

The surface is created with a format. The FORMAT_RGB24 format creates a normal RGB image. It also accepts a width and height, specifying the size of the output image in pixels.

There are a few other things we should know about ImageSurface.

Formats

...