osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Generating SVG from turtle graphics


On 2018-01-11 12:03, Steven D'Aprano wrote:
> I'd like to draw something with turtle, then generate a SVG file from it.
> 
> Is this possible?
> 
> If not, is there something I can do which lets me plot lines, shapes and 
> curves and output to SVG?
> 
> Ideally, I'd like to draw a figure pixel by pixel, and then have the SVG 
> library fit a bezier curve to it.
> 

You *could* use matplotlib, which can export to SVG.

Of course, the API is far from ideal for drawing arbitrary shapes --
though it's naturally quite good with lines.

There are some helpful examples in the docs:

https://matplotlib.org/examples/
https://matplotlib.org/examples/shapes_and_collections/index.html

https://matplotlib.org/api/patches_api.html
https://matplotlib.org/api/pyplot_summary.html
... and so on ...


I just played around with it for a few minutes to get a feel for how
much boilerplate you need -

###################################################

from matplotlib import pyplot as plt
from matplotlib.patches import Circle, Rectangle
import numpy as np

ax = plt.gca()
ax.set_ylim([-2, 2])
ax.set_xlim([-2, 2])
ax.set_aspect('equal')
plt.axis('off')

circle = Circle([0,0], 1, fc='red', ec='none')
halfdiag = np.sqrt(0.5)
rect = Rectangle([-halfdiag, -halfdiag],
                 2*halfdiag, 2*halfdiag,
                 fc='yellow', ec='none')

ax.add_patch(circle)
ax.add_patch(rect)

plt.savefig('/tmp/test.svg')

###################################################

- and this gave a reasonable-looking SVG -

###################################################

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd";>
<!-- Created with matplotlib (http://matplotlib.org/) -->
<svg height="345pt" version="1.1" viewBox="0 0 460 345" width="460pt"
xmlns="http://www.w3.org/2000/svg";
xmlns:xlink="http://www.w3.org/1999/xlink";>
 <defs>
  <style type="text/css">
*{stroke-linecap:butt;stroke-linejoin:round;}
  </style>
 </defs>
 <g id="figure_1">
  <g id="patch_1">
   <path d="M 0 345.6
L 460.8 345.6
L 460.8 0
L 0 0
z
" style="fill:#ffffff;"/>
  </g>
  <g id="axes_1">
   <g id="patch_2">
    <path clip-path="url(#p8457ab97e8)" d="M 236.16 241.056
C 253.803432 241.056 270.72661 234.04619 283.2024 221.5704
C 295.67819 209.09461 302.688 192.171432 302.688 174.528
C 302.688 156.884568 295.67819 139.96139 283.2024 127.4856
C 270.72661 115.00981 253.803432 108 236.16 108
C 218.516568 108 201.59339 115.00981 189.1176 127.4856
C 176.64181 139.96139 169.632 156.884568 169.632 174.528
C 169.632 192.171432 176.64181 209.09461 189.1176 221.5704
C 201.59339 234.04619 218.516568 241.056 236.16 241.056
z
" style="fill:#ff0000;"/>
   </g>
   <g id="patch_3">
    <path clip-path="url(#p8457ab97e8)" d="M 189.1176 221.5704
L 283.2024 221.5704
L 283.2024 127.4856
L 189.1176 127.4856
z
" style="fill:#ffff00;"/>
   </g>
  </g>
 </g>
 <defs>
  <clipPath id="p8457ab97e8">
   <rect height="266.112" width="266.112" x="103.104" y="41.472"/>
  </clipPath>
 </defs>
</svg>