Python SVG

电脑技术 电脑技术 2372 人阅读 | 1 人回复 | 2022-04-15

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import drawSvg as draw

  2. d = draw.Drawing(200, 100, origin='center', displayInline=False)

  3. # Draw an irregular polygon
  4. d.append(draw.Lines(-80, -45,
  5.                     70, -49,
  6.                     95, 49,
  7.                     -90, 40,
  8.                     close=False,
  9.             fill='#eeee00',
  10.             stroke='black'))

  11. # Draw a rectangle
  12. r = draw.Rectangle(-80,0,40,50, fill='#1248ff')
  13. r.appendTitle("Our first rectangle")  # Add a tooltip
  14. d.append(r)

  15. # Draw a circle
  16. d.append(draw.Circle(-40, -10, 30,
  17.             fill='red', stroke_width=2, stroke='black'))

  18. # Draw an arbitrary path (a triangle in this case)
  19. p = draw.Path(stroke_width=2, stroke='lime',
  20.               fill='black', fill_opacity=0.2)
  21. p.M(-10, 20)  # Start path at point (-10, 20)
  22. p.C(30, -10, 30, 50, 70, 20)  # Draw a curve to (70, 20)
  23. d.append(p)

  24. # Draw text
  25. d.append(draw.Text('Basic text', 8, -10, 35, fill='blue'))  # Text with font size 8
  26. d.append(draw.Text('Path text', 8, path=p, text_anchor='start', valign='middle'))
  27. d.append(draw.Text(['Multi-line', 'text'], 8, path=p, text_anchor='end'))

  28. # Draw multiple circular arcs
  29. d.append(draw.ArcLine(60,-20,20,60,270,
  30.             stroke='red', stroke_width=5, fill='red', fill_opacity=0.2))
  31. d.append(draw.Arc(60,-20,20,60,270,cw=False,
  32.             stroke='green', stroke_width=3, fill='none'))
  33. d.append(draw.Arc(60,-20,20,270,60,cw=True,
  34.             stroke='blue', stroke_width=1, fill='black', fill_opacity=0.3))

  35. # Draw arrows
  36. arrow = draw.Marker(-0.1, -0.5, 0.9, 0.5, scale=4, orient='auto')
  37. arrow.append(draw.Lines(-0.1, -0.5, -0.1, 0.5, 0.9, 0, fill='red', close=True))
  38. p = draw.Path(stroke='red', stroke_width=2, fill='none',
  39.               marker_end=arrow)  # Add an arrow to the end of a path
  40. p.M(20, -40).L(20, -27).L(0, -20)  # Chain multiple path operations
  41. d.append(p)
  42. d.append(draw.Line(30, -20, 0, -10,
  43.             stroke='red', stroke_width=2, fill='none',
  44.             marker_end=arrow))  # Add an arrow to the end of a line

  45. d.setPixelScale(2)  # Set number of pixels per geometry unit
  46. #d.setRenderSize(400,200)  # Alternative to setPixelScale
  47. d.saveSvg('example.svg')
  48. d.savePng('example.png')

  49. # Display in Jupyter notebook
  50. d.rasterize()  # Display as PNG
  51. d  # Display as SVG
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐