gpt4 book ai didi

python - 如何将文本转换为路径?

转载 作者:太空狗 更新时间:2023-10-29 21:08:48 26 4
gpt4 key购买 nike

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

Text = 'Welcome to 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")

最佳答案

您可以使用 inkscape:

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

注意:你必须先安装inkscape

关于python - 如何将文本转换为路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18057911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com