svg - 如何将形状优美的多边形转换为 SVG?

标签 svg shapely

我可以通过以下方式创建多边形:

#!/usr/bin/env python

from shapely.geometry import Polygon

area = Polygon(((52, 13), (57, 14), (58, 12)))

with open('test.svg', 'w') as f:
    f.write(area.svg())

返回

<path fill-rule="evenodd" fill="#66cc99" stroke="#555555" stroke-width="2.0" opacity="0.6" d="M 52.0,13.0 L 57.0,14.0 L 58.0,12.0 L 52.0,13.0 z" />

这不是有效的 SVG 文件。如何获得有效的 SVG?

我试过了

#!/usr/bin/env python

from shapely.geometry import Polygon

area = Polygon(((52, 13), (57, 14), (58, 12)))

with open('test.svg', 'w') as f:
    f.write('<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink= "http://www.w3.org/1999/xlink">')
    f.write(area.svg())
    f.write('</svg>')

当我查看它时,视口(viewport)对于多边形来说太大了。使用 Inkscape 手动编辑它并调整它的大小:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   version="1.1"
   id="svg2"
   inkscape:version="0.91 r13725"
   sodipodi:docname="test.svg"
   width="7.9687681"
   height="4.4396091">
  <metadata
     id="metadata10">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
        <dc:title></dc:title>
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <defs
     id="defs8" />
  <sodipodi:namedview
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1"
     objecttolerance="10"
     gridtolerance="10"
     guidetolerance="10"
     inkscape:pageopacity="0"
     inkscape:pageshadow="2"
     inkscape:window-width="2560"
     inkscape:window-height="1364"
     id="namedview6"
     showgrid="false"
     inkscape:zoom="2.36"
     inkscape:cx="-1.8038839"
     inkscape:cy="-34.869627"
     inkscape:window-x="0"
     inkscape:window-y="24"
     inkscape:window-maximized="1"
     inkscape:current-layer="svg2"
     fit-margin-top="0"
     fit-margin-left="0"
     fit-margin-right="0"
     fit-margin-bottom="0" />
  <path
     d="m 0.19611614,2.3092357 4.99999996,1 1,-2 -5.99999996,1 z"
     id="path4"
     inkscape:connector-curvature="0"
     style="opacity:0.6;fill:#66cc99;fill-rule:evenodd;stroke:#555555;stroke-width:2" />
</svg>

有没有办法自动获取这个?

最佳答案

尝试:

with open('test.svg', 'w') as f:
    f.write(area._repr_svg_())

BaseGeometry._repr_svg_ 函数用于 IPython/Jupyter 集成,以便在 Jupyter 笔记本中内联渲染 Shapely 对象。因此这里定义了“内部”命名: http://ipython.readthedocs.io/en/stable/api/generated/IPython.display.html

实际上,它会生成有效的 SVG 输出。

关于svg - 如何将形状优美的多边形转换为 SVG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49147707/

相关文章:

svg - 三个js线框球体svg

javascript - html2canvas 下载带有 SVG 背景图像的图像

android - iOS Swift 像 Android 一样将 SVG 图像添加到按钮

ios - 如何在 ios sdk 中将 svg 图像设置为按钮图像?

javascript - 避免在响应式 SVG 上拉伸(stretch)细长边框

python - 如何将 LineString 拆分为段

python - 为什么在某些情况下转换形状多边形不起作用?

Python shapely : . equals 函数并不总是有效:

python - shapely 有信封类吗?

python - 安装匀称: OSError: [WinError 126] The specified module could not be found