def image2surface(img):
"""
Convert a PIL image into a Cairo surface
"""
if not CAIRO_AVAILABLE:
raise Exception("Cairo not available(). image2surface() cannot work.")
# TODO(Jflesch): Python 3 problem
# cairo.ImageSurface.create_for_data() raises NotImplementedYet ...
# img.putalpha(256)
# (width, height) = img.size
# imgd = img.tobytes('raw', 'BGRA')
# imga = array.array('B', imgd)
# stride = width * 4
# return cairo.ImageSurface.create_for_data(
# imga, cairo.FORMAT_ARGB32, width, height, stride)
# So we fall back to this method:
global g_lock
with g_lock:
img_io = io.BytesIO()
img.save(img_io, format="PNG")
img_io.seek(0)
return cairo.ImageSurface.create_from_png(img_io)
评论列表
文章目录