python - 在 Python 中显示 SVG 文件

标签 python matplotlib svg

我正在创建一个 Python3 程序,该程序生成我个人项目所需的带有随机圆圈的 SVG 图像。现在该程序运行良好,但我想微调一些参数以产生最佳结果。我的想法是有一个显示中间结果的显示窗口和一个交互式外壳来更改生成圆圈位置的一些值,而不必创建数千个不同的图像并进行比较。完成后,我可以简单地保存结果。

问题来了:我真的不知道如何实现这个结果。我首先想到使用 Matplotlib,它通常非常适合这类任务,但它似乎无法读取 SVG 文件。我也想过直接把圆圈画成plot,但是有的要模糊(SVG文件中的Gaussian blur filter),Matplotlib好像不能模糊形状。最后,我寻找其他能够显示 SVG 文件或直接显示程序生成的文本以生成 SVG 文件的东西,但没有成功。

编辑:我刚刚尝试使用 CairoSVG 将 SVG 文件转换为 PNG。它有效,但它不支持高斯模糊,因此它是一个次优解决方案。我可以接受涉及将 SVG 文件转换为 PNG 或其他格式的解决方案,但我希望它至少支持高斯模糊,否则我可以简单地使用 Matplotlib 绘制每个圆圈。

在此先感谢您的帮助!

最佳答案

这是一个使用 PyQt5(或 PySide2 在我的例子中)和 QWebEngineView 的解决方案(提到 herethere 解释了为什么 QSvgWidget 不处理像模糊这样的过滤器)。

这只是显示 svg(带有模糊),您可能希望添加小部件以使其具有交互性,如您所愿:

import sys

from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtNetwork import QNetworkProxy, QNetworkProxyFactory
from PySide2.QtWebEngineWidgets import QWebEngineView


class DisplaySVG(QtWidgets.QWidget):
    "A simple SVG display."
    def __init__(self, url=None, parent=None):
        super().__init__(parent)
        self.resize(800,600)
        self.verticalLayout = QtWidgets.QVBoxLayout(self)
        self.webview = QWebEngineView(self)
        self.verticalLayout.addWidget(self.webview)

        self.setWindowTitle("Display SVG")
        act = QtWidgets.QAction("Close", self)
        act.setShortcuts([QtGui.QKeySequence(QtCore.Qt.Key_Escape)])
        act.triggered.connect(self.close)
        self.addAction(act)

        svg = '''
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<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"
   width="210mm"
   height="297mm"
   viewBox="0 0 210 297"
   version="1.1"
   id="svg8"
   inkscape:version="0.92.4 (5da689c313, 2019-01-14)"
   sodipodi:docname="drawing.svg">
  <defs
     id="defs2">
    <filter
       style="color-interpolation-filters:sRGB;"
       inkscape:label="Blur"
       id="filter4530">
      <feGaussianBlur
         stdDeviation="3.48559 2"
         result="fbSourceGraphic"
         id="feGaussianBlur4528" />
      <feColorMatrix
         result="fbSourceGraphicAlpha"
         in="fbSourceGraphic"
         values="0 0 0 -1 0 0 0 0 -1 0 0 0 0 -1 0 0 0 0 1 0"
         id="feColorMatrix4532" />
      <feGaussianBlur
         id="feGaussianBlur4534"
         stdDeviation="3.49 2"
         result="blur"
         in="fbSourceGraphic" />
    </filter>
  </defs>
  <sodipodi:namedview
     id="base"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageopacity="0.0"
     inkscape:pageshadow="2"
     inkscape:zoom="0.7"
     inkscape:cx="214.06823"
     inkscape:cy="366.85869"
     inkscape:document-units="mm"
     inkscape:current-layer="layer1"
     showgrid="false"
     inkscape:window-width="1487"
     inkscape:window-height="958"
     inkscape:window-x="58"
     inkscape:window-y="85"
     inkscape:window-maximized="0" />
  <metadata
     id="metadata5">
    <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>
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <circle
       style="opacity:1;fill:#ff5555;fill-opacity:0.57480317;stroke:#2c2cff;stroke-width:1.882;stroke-miterlimit:4;stroke-dasharray:3.764, 1.88199999999999990;stroke-dashoffset:0;stroke-opacity:1;filter:url(#filter4530)"
       id="path4518"
       cx="66.523811"
       cy="123.13095"
       r="36.285713" />
  </g>
</svg>
        '''

        self.webview.setHtml(svg)

qt_app = QtWidgets.QApplication(sys.argv)
disp = DisplaySVG()
disp.show()
qt_app.exec_()

关于python - 在 Python 中显示 SVG 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63139025/

相关文章:

python - py2neo (1.6) 如何在没有 CypherQuery 的情况下逐个节点属性获取现有节点

python - 对不带年、月、日的时间戳数组进行算术

python - 基于字典向数据框添加新列

python - python matplotlib 向 x 轴添加更多标签

python - 根据给定值向 matplotlib 中的线图添加颜色

python - Raspberry Pi 上的按钮交互

python/matplotlib/seaborn- x 轴上带有数据点的箱线图

css - 线条动画 SVG,动画不起作用

CSS 背景悬停过渡与 IE 中的 svg 悬停过渡不同步

javascript - 在 SVG "Path"元素/三 Angular 形的两侧生成内阴影效果