image-processing - 如何在 pycairo 中读取 svg 数据笔划?

标签 image-processing svg pycairo

我有 JPG 图像和 inputvgdraw,一个用于图像注释的 Flash 工具 (http://www.mainada.net/inputdraw),我可以在其上跟踪生成 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 中的注释,您可能需要使用 PILnumpycairo本身不会加载许多不同的图像格式。

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

import sys
import rsvg
import cairo
import numpy
from PIL import Image

# Load an image that supposedly has the same width and height as the svg one.
img_rgba = numpy.array(Image.open(sys.argv[1]).convert('RGBA'))
data = numpy.array(img_rgba.tostring('raw', 'BGRA'))
width, height = img_rgba.size

surface = cairo.ImageSurface.create_for_data(data,
        cairo.FORMAT_ARGB32, width, height)
ctx = cairo.Context(surface)

# "Paste" the svg into the image.
svg = rsvg.Handle(file=sys.argv[2])
svg.render_cairo(ctx)

surface.write_to_png(sys.argv[3])

关于image-processing - 如何在 pycairo 中读取 svg 数据笔划?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14927840/

相关文章:

html - 使用图像标签在网页上显示原始像素阵列

css - 使用变换属性创建背景

python - 如何结合 PIL 和 cairosvg 从 svg 创建模式?

python - 在 PyGame (SDL) 中使用 Cairo 时的字节顺序

c++ - 输出为梯度计算中提供的单个图像并排生成 4 个图像

c++ - OpenCV:将 3 channel 图像转换为 4 channel

java - 在 ANDroid 中读取和写入图像到 SDCard

html - SVG 元素无法在 Safari 中显示阴影过滤器

javascript - 内联 svg 作为指令(Firefox 问题)

python - pip 安装失败,出现 FileNotFoundError : setup. py