likes
comments
collection
share

python由svg生成turtle代码

作者站长头像
站长
· 阅读数 54

python由svg生成turtle代码

最终目的

具体就是将svg中的(目前只有)线条line、矩形rect、圆circle由turtle画出来。

实现

这里需要一个python库。 xml.etree.ElementTree(以下简称et) 是一个解析xml的库, svg是xml的一种表达,所以我们会有用到et。

import turtle
import xml.etree.ElementTree as ET
print('自动输出svg转python_turtle脚本(©Elan_x)\n'
      'import turtle')
 
def draw_svg(svg_file):
    # 创建turtle画布
    screen = turtle.Screen()
    screen.setup(800, 600)
    screen.bgcolor("white")
 
    # 创建turtle对象
    t = turtle.Turtle()
    t.speed(0)  # 设置速度为最快,如果想要慢慢欣赏可以调节速度
    print('''
screen = turtle.Screen()
screen.setup(800, 600)
screen.bgcolor("white")
t = turtle.Turtle()
t.speed(0)
    ''')
    # 解析SVG文件
    tree = ET.parse(svg_file)
    root = tree.getroot()
 
    # 绘制SVG元素
    for element in root.iter():
        tag = element.tag.split('}')[-1]
        if tag == 'circle':
            draw_circle(t, element.attrib)
        elif tag == 'rect':
            draw_rect(t, element.attrib)
        elif tag == 'line':
            draw_line(t, element.attrib)
            # 可以继续添加其他元素的绘制函数
 
    # 隐藏turtle并显示绘制结果
    t.hideturtle()
    turtle.done()
    print('''
t.hideturtle()
turtle.done() 
    ''')
 
def draw_circle(t, attribs):
    cx = 0 - (float(attribs['cx']) - 400)
    cy = 0 - (float(attribs['cy']) - 300)
    r = float(attribs['r'])
    t.penup()
    t.goto(cx, cy)
    t.pendown()
    t.circle(r)
    print(f'''
t.penup()
t.goto({cx}, {cy})
t.pendown()
t.circle({r})
    ''')
 
 
def draw_rect(t, attribs):
    x = float(attribs['x']) - 400
    y = 0 - (float(attribs['y']) - 300)
    width = float(attribs['width'])
    height = float(attribs['height'])
    t.penup()
    t.goto(x, y)
    t.pendown()
    for _ in range(2):
        t.forward(width)
        t.right(90)
        t.forward(height)
        t.right(90)
    print(f'''
t.penup()
t.goto({x}, {y})
t.pendown()
for _ in range(2):
    t.forward({width})
    t.right(90)
    t.forward({height})
    t.right(90)
    ''')
 
 
def draw_line(t, attrib):
    x1 = float(attrib['x1']) - 400
    y1 = 0 - (float(attrib['y1']) - 300)
    x2 = float(attrib['x2']) - 400
    y2 = 0 - (float(attrib['y2']) - 300)
    t.penup()
    t.goto(x1, y1)
    t.pendown()
    t.goto(x2, y2)
    print(f'''
t.penup()
t.goto({x1}, {y1})
t.pendown()
t.goto({x2}, {y2})
    ''')
 
# 可以继续添加其他元素的绘制函数
 
if __name__ == "__main__":
    svg_file = r"svg_path"
    draw_svg(svg_file)
    print('输出完毕')

原理很简单, 程序读取svg内容并递交给函数进行绘制。 turtle绘画时每执行一步就把那一步所执行的脚本print出来。 我制作了这样一个svg图片,左侧两个字符由线段组成,用于测试line的读取绘制; 字符"A"由矩形组成,用于测试rect的读取绘制; 最右侧字符由不支持的格式组成,用于测试是否发生错误。 python由svg生成turtle代码 最终效果:

import turtle
 
screen = turtle.Screen()
screen.setup(800, 600)
screen.bgcolor("white")
 
t = turtle.Turtle()
t.speed(0)
 
t.penup()
t.goto(-264.0, 168.0)
t.pendown()
t.goto(-249.0, -82.0)
 
t.penup()
t.goto(-189.0, 167.0)
t.pendown()
t.goto(-261.0, 164.0)
 
t.penup()
t.goto(-178.0, 86.0)
t.pendown()
t.goto(-248.0, 61.0)
 
t.penup()
t.goto(-179.0, -38.0)
t.pendown()
t.goto(-245.0, -65.0)
 
t.penup()
t.goto(-134.0, 149.0)
t.pendown()
t.goto(-139.0, -42.0)
 
t.penup()
t.goto(-135.0, -60.0)
t.pendown()
t.goto(-78.0, -29.0)
 
t.penup()
t.goto(-20.0, 169.0)
t.pendown()
for _ in range(2):
    t.forward(39.0)
    t.right(90)
    t.forward(301.0)
    t.right(90)
 
t.penup()
t.goto(-23.0, 209.0)
t.pendown()
for _ in range(2):
    t.forward(142.0)
    t.right(90)
    t.forward(37.0)
    t.right(90)
 
t.penup()
t.goto(75.0, 155.0)
t.pendown()
for _ in range(2):
    t.forward(41.0)
    t.right(90)
    t.forward(291.0)
    t.right(90)
 
t.penup()
t.goto(28.0, 59.0)
t.pendown()
for _ in range(2):
    t.forward(36.0)
    t.right(90)
    t.forward(46.0)
    t.right(90)
 
t.hideturtle()
turtle.done()

python由svg生成turtle代码 完美运行

转载自:https://juejin.cn/post/7377595015047315467
评论
请登录