【问题标题】:How to convert text to paths?如何将文本转换为路径?
【发布时间】:2013-08-05 12:15:18
【问题描述】:

我正在尝试将文本转换为曲线和路径,例如:

Text = '欢迎使用 python'

我正在尝试将此文本转换为路径。 我也试图将此路径信息作为点列表获取。

我想将文本作为路径存储在 SVG 文件中。

例如,当您在 adobe illustrator 中将文本转换为轮廓时。

我试过这个例子,但这不是我想要的: cairo example

import cairo

def text_extent(font, font_size, text, *args, **kwargs):
    surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 0, 0)
    ctx = cairo.Context(surface)
    ctx.select_font_face(font, *args, **kwargs)
    ctx.set_font_size(font_size)
    return ctx.text_extents(text)

text='Example'
font="Sans"
font_size=55.0
font_args=[cairo.FONT_SLANT_NORMAL]
(x_bearing, y_bearing, text_width, text_height,
 x_advance, y_advance) = text_extent(font, font_size, text, *font_args)
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(text_width), int(text_height))
ctx = cairo.Context(surface)
ctx.select_font_face(font, *font_args)
ctx.set_font_size(font_size)
ctx.move_to(-x_bearing, -y_bearing)
ctx.text_path(text)
ctx.set_source_rgb(0.47, 0.47, 0.47)
ctx.fill_preserve()
ctx.set_source_rgb(1, 0, 0)
ctx.set_line_width(1.5)
ctx.stroke()

surface.write_to_png("/tmp/out.png")

【问题讨论】:

  • 为什么这个例子没有达到你想要的效果?
  • 因为我想将文本转换为路径而不仅仅是概述文本我希望路径数据作为点列表能够将其作为路径存储在 svg 文件中
  • @TawfiqabuHalawah 如果你找到了解决办法,可以发帖吗?

标签: python text path curves


【解决方案1】:

你可以使用inkscape:

import subprocess
subprocess.call("inkscape in.svg --export-text-to-path --export-plain-svg out.svg", shell = True)

注意:你必须先安装inkscape

【讨论】:

    猜你喜欢
    • 2011-12-06
    • 2014-04-27
    • 2020-05-15
    • 2014-12-23
    • 2013-12-13
    • 2012-03-30
    • 2015-04-15
    • 1970-01-01
    • 2011-01-19
    相关资源
    最近更新 更多