将PIL图像转换为Cairo图像

2024-10-01 13:42:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从PIL图像创建cairo ImageSurface,到目前为止,我的代码是:

im = Image.open(filename)
imstr = im.tostring()
a = array.array('B', imstr)
height, width = im.size
stride = cairo.ImageSurface.format_stride_for_width(cairo.FORMAT_RGB24, width)
return cairo.ImageSurface.create_for_data(a, cairo.FORMAT_ARGB24, width, height, stride)

但这给了我

^{pr2}$

我真的不明白为什么会这样,也许我对图像格式还不够了解。在

我用的是cairo1.10。在


Tags: 代码图像imageformatforpilopenwidth
2条回答

Cairo的create_for_data()is需要一个可写的缓冲区对象(字符串可以用作缓冲区对象,但不可写),并且它只支持32位/像素的数据(RGBA或RGB后跟一个未使用的字节)。另一方面,PIL提供一个24bpp的RGB只读缓冲区对象。在

我建议您告诉PIL添加一个alpha通道,然后将PIL缓冲区转换为numpy数组,以获得Cairo的可写缓冲区。在

im = Image.open(filename)
im.putalpha(256) # create alpha channel
arr = numpy.array(im)
height, width, channels = arr.shape
surface = cairo.ImageSurface.create_for_data(arr, cairo.FORMAT_RGB24, width, height)

如果出现以下情况,接受的版本将无法正常工作:

  • 你的图像有颜色
  • 你的形象不是不透明的
  • 您的图像处于不同于RGB(a)的模式

在cairo中,图像颜色的值由alpha值预乘,并使用本机CPU endianness将它们存储为32位字。这意味着PIL图像:

r1 g1 b1 a1 r2 g2 b2 a2 ...

存储在cairo的一个小型endian CPU中:

^{pr2}$

在大端CPU中:

a1 r1*a1 b1*a1 g1*a1 a2 r2*a2 g2*a2 b2*a2 ...

以下是一个在没有NumPy依赖的小型endian机器上正常工作的版本:

def pil2cairo(im):
    """Transform a PIL Image into a Cairo ImageSurface."""

    assert sys.byteorder == 'little', 'We don\'t support big endian'
    if im.mode != 'RGBA':
        im = im.convert('RGBA')

    s = im.tostring('raw', 'BGRA')
    a = array.array('B', s)
    dest = cairo.ImageSurface(cairo.FORMAT_ARGB32, im.size[0], im.size[1])
    ctx = cairo.Context(dest)
    non_premult_src_wo_alpha = cairo.ImageSurface.create_for_data(
        a, cairo.FORMAT_RGB24, im.size[0], im.size[1])
    non_premult_src_alpha = cairo.ImageSurface.create_for_data(
        a, cairo.FORMAT_ARGB32, im.size[0], im.size[1])
    ctx.set_source_surface(non_premult_src_wo_alpha)
    ctx.mask_surface(non_premult_src_alpha)
    return dest

这里我做开罗的预乘。我也试过用NumPy做预乘,但结果比较慢。在我的电脑(Mac OS X,2.13GHz Intel Core 2 Duo)中,这个函数需要~1秒来转换6000x6000像素的图像,而5毫秒来转换500x500像素的图像。在

相关问题 更多 >