我正在尝试从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。在
Cairo的create_for_data()is需要一个可写的缓冲区对象(字符串可以用作缓冲区对象,但不可写),并且它只支持32位/像素的数据(RGBA或RGB后跟一个未使用的字节)。另一方面,PIL提供一个24bpp的RGB只读缓冲区对象。在
我建议您告诉PIL添加一个alpha通道,然后将PIL缓冲区转换为numpy数组,以获得Cairo的可写缓冲区。在
如果出现以下情况,接受的版本将无法正常工作:
在cairo中,图像颜色的值由alpha值预乘,并使用本机CPU endianness将它们存储为32位字。这意味着PIL图像:
存储在cairo的一个小型endian CPU中:
^{pr2}$在大端CPU中:
以下是一个在没有NumPy依赖的小型endian机器上正常工作的版本:
这里我做开罗的预乘。我也试过用NumPy做预乘,但结果比较慢。在我的电脑(Mac OS X,2.13GHz Intel Core 2 Duo)中,这个函数需要~1秒来转换6000x6000像素的图像,而5毫秒来转换500x500像素的图像。在
相关问题 更多 >
编程相关推荐