Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image processing 如何在pycairo中读取svg数据?_Image Processing_Svg_Pycairo - Fatal编程技术网

Image processing 如何在pycairo中读取svg数据?

Image processing 如何在pycairo中读取svg数据?,image-processing,svg,pycairo,Image Processing,Svg,Pycairo,我有JPG图像,通过inputsvgdraw(一个用于图像注释()的flash工具),我可以跟踪图像上生成svg数据的行 svg数据示例: <svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 488 325"><g fill="none" stroke-miterlimit="6" stroke-linecap="round" stroke-linejoin="round"><pa

我有JPG图像,通过inputsvgdraw(一个用于图像注释()的flash工具),我可以跟踪图像上生成svg数据的行

svg数据示例:

<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 488 325"><g fill="none"   stroke-miterlimit="6" stroke-linecap="round" stroke-linejoin="round"><path d="M 307 97 l 0 -1 l -2 -1 l -10 -2 l -20 -1 l -25 5 l -22 9 l -10 9 l 0 9 l 2 12 l 16 18 l 25 11 l 25 5 l 17 -1 l 6 -4 l 3 -7 l -1 -12 l -6 -16 l -7 -13 l -11 -12 l -11 -14 l -9 -5" opacity="1" stroke="rgb(170,37,34)" stroke-width="5"/></g></svg>.

什么功能可以管理这些数据

您可以使用
librsvg
读取SVG输入,然后使用
cairo
进行渲染。如果要在初始图像上绘制SVG注释,可能需要将
PIL
numpy
一起使用,因为
cairo
本身不会加载许多不同的图像格式

下面是一个实现这一点的示例(唯一的区别是,实际上我用一个特设的
ctypes
包装器对
rsvg
进行了测试):

可以将SVG文件加载到pycairo ImageSurface:

from cairocffi import ImageSurface
from cairosvg.parser import Tree
from cairosvg.surface import PNGSurface


def load_svg(svg: bytes) -> ImageSurface:
    return PNGSurface(Tree(bytestring=svg), None, 1).cairo
我使用PNGSurface,因为它将数据渲染到ImageSurface。这里不使用PNG功能

不幸的是cairosvg使用的是pycairo而不是pycairo。这就是为什么我把我的整个项目改成Cairoffi。这只需要采用进口和一个微小的变化

from cairocffi import ImageSurface
from cairosvg.parser import Tree
from cairosvg.surface import PNGSurface


def load_svg(svg: bytes) -> ImageSurface:
    return PNGSurface(Tree(bytestring=svg), None, 1).cairo