【问题标题】:Display SVG file in Python在 Python 中显示 SVG 文件
【发布时间】:2020-11-18 04:23:55
【问题描述】:

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

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

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

提前感谢您的帮助!

【问题讨论】:

  • 我刚刚尝试了 PyQt5 和 QSvgWidget,跟随 this idea;但它也不显示模糊。

标签: python matplotlib svg


【解决方案1】:

这是一个使用 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_()

【讨论】:

  • 它肯定能正确显示 SVG 文件。我将不得不稍微研究一下 PyQt5 文档,看看我是否可以获得类似于plt.ion() 的内容,以便在窗口打开时工作并刷新图像,但至少我现在有了一个起点。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 2017-07-19
  • 2018-10-20
  • 2010-10-31
  • 2011-07-16
  • 1970-01-01
  • 2019-10-10
相关资源
最近更新 更多