Storing Image Data
Learn about how Pycairo stores image data.
We'll cover the following...
Pycairo can be integrated with several other Python libraries to extend its capabilities. We will look at some examples in this section.
How does Pycairo store image data?
Several of the techniques for transferring image data make use of the ImageSurface
methods: create_for_data
and get_data
.
create_for_data
accepts a writable buffer or memoryview
object that contains image data. It also requires the format,such as the cairo.Format class=RGB24
format, and the image width
and height
.
surface = cairo.ImageSurface.create_for_data(data, format, width, height, [stride])
This creates a new ImageSurface
, which contains the image contained in the data. Stride
is an optional integer that indicates the number of bytes between the start of each row in the data buffer. It can be ignored and the function will calculate the value based on the image width.
The get_data
function returns a memoryview
object, which contains image data from the ImageSurface
:
data = surface.get_data()
Over here, the surface
is an existing ImageSurface
.
This is a playground ...