集成 NumPy

集成 NumPy#

从 NumPy 数组创建 ImageSurface:

import numpy
import cairo

width, height = 255, 255
data = numpy.ndarray(shape=(height, width), dtype=numpy.uint32)
surface = cairo.ImageSurface.create_for_data(
    data, cairo.FORMAT_ARGB32, width, height
)
surface
<cairo.ImageSurface at 0x7ffbc5337ef0>

从 ImageSurface 创建 NumPy 数组:

import numpy
import cairo

width, height = 255, 255
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
buf = surface.get_data()
data = numpy.ndarray(shape=(height, width),
                        dtype=numpy.uint32,
                        buffer=buf)