Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. [PySide] How to save svg to png?

[PySide] How to save svg to png?

Scheduled Pinned Locked Moved Unsolved Qt for Python
10 Posts 3 Posters 922 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    fzet
    wrote on last edited by
    #1

    I have an svg with css styles (also font-face contains local ttf fonts)
    How to save as png?

    1 Reply Last reply
    0
    • F Offline
      F Offline
      fzet
      wrote on last edited by fzet
      #2

      found solution

      from PySide2.QtCore import QUrl
      from PySide2.QtGui import QImage, QPainter
      from PySide2.QtSvg import QSvgRenderer
      from PySide2.QtWidgets import QMainWindow, QApplication
      from PySide2.QtWebEngineWidgets import QWebEngineView
      
      class MainWindow(QMainWindow):
          def __init__(self, *args, **kwargs):
              super(MainWindow, self).__init__(*args, **kwargs)
      
              path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "dali.svg")
              s = QSvgRenderer(path)
              i = QImage(742, 10116 , QImage.Format_ARGB32)
              p = QPainter(i)
              s.render(p)
              i.save('./test.png')
              p.end()
      
      
      app = QApplication(sys.argv)
      window = MainWindow()
      window.show()
      
      app.exec_()
      
      

      Does QSvgRenderer supports external fonts and css styles? (like font-face)

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        As stated in the documentation, QtSvg support only the static features of SVG 1.2 tiny.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        F 2 Replies Last reply
        1
        • SGaistS SGaist

          Hi,

          As stated in the documentation, QtSvg support only the static features of SVG 1.2 tiny.

          F Offline
          F Offline
          fzet
          wrote on last edited by fzet
          #4

          @SGaist Thank you!
          SVG 1.2 tiny supported font-face.
          So what's wrong in my code?
          My SVG has css style:

          @font-face {
              font-family: 'Hack';
              src: url('./Hack.ttf');
          }
          

          But it doesn't work
          Maybe it has some security restrictions for adding remote files?

          And please tell me ,how to add or apply css styles ?

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            As stated in the documentation, QtSvg support only the static features of SVG 1.2 tiny.

            F Offline
            F Offline
            fzet
            wrote on last edited by fzet
            #5

            QWebEngineView shows all remote fonts correctly, but QSvgRenderer renders the default font...

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              QtWebEngine is a web browser that implements the whole specification. From a cursorary look, QtSvg does not handle font-face.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              F 2 Replies Last reply
              1
              • SGaistS SGaist

                QtWebEngine is a web browser that implements the whole specification. From a cursorary look, QtSvg does not handle font-face.

                F Offline
                F Offline
                fzet
                wrote on last edited by
                #7

                @SGaist
                so sad :(
                can I render my svg in QtWebEngine and then save it to png?

                1 Reply Last reply
                0
                • SGaistS SGaist

                  QtWebEngine is a web browser that implements the whole specification. From a cursorary look, QtSvg does not handle font-face.

                  F Offline
                  F Offline
                  fzet
                  wrote on last edited by
                  #8

                  @SGaist
                  I found this example:

                  i = QImage(746, 516, QImage.Format_ARGB32)
                  p = QPainter(i)
                  self.browser.page().view().render(p)
                  

                  But it doesn't work:

                  TypeError: 'PySide2.QtWidgets.QWidget.render' called with wrong argument types:
                    PySide2.QtWidgets.QWidget.render(QPainter)
                  Supported signatures:
                    PySide2.QtWidgets.QWidget.render(PySide2.QtGui.QPainter, PySide2.QtCore.QPoint, PySide2.QtGui.QRegion = Default(QRegion), PySide2.QtWidgets.QWidget.RenderFlags = Instance(QWidget.RenderFlags(QWidget.DrawWindowBackground | QWidget.DrawChildren)))
                    PySide2.QtWidgets.QWidget.render(PySide2.QtGui.QPaintDevice, PySide2.QtCore.QPoint = Default(QPoint), PySide2.QtGui.QRegion = Default(QRegion), PySide2.QtWidgets.QWidget.RenderFlags = Instance(QWidget.RenderFlags(QWidget.DrawWindowBackground | QWidget.DrawChildren)))
                  Release of profile requested but WebEnginePage still not deleted. Expect troubles !
                  QPaintDevice: Cannot destroy paint device that is being painted
                  Segmentation fault (core dumped)
                  

                  type p == PySide2.QtGui.QPainter

                  JonBJ 1 Reply Last reply
                  0
                  • F fzet

                    @SGaist
                    I found this example:

                    i = QImage(746, 516, QImage.Format_ARGB32)
                    p = QPainter(i)
                    self.browser.page().view().render(p)
                    

                    But it doesn't work:

                    TypeError: 'PySide2.QtWidgets.QWidget.render' called with wrong argument types:
                      PySide2.QtWidgets.QWidget.render(QPainter)
                    Supported signatures:
                      PySide2.QtWidgets.QWidget.render(PySide2.QtGui.QPainter, PySide2.QtCore.QPoint, PySide2.QtGui.QRegion = Default(QRegion), PySide2.QtWidgets.QWidget.RenderFlags = Instance(QWidget.RenderFlags(QWidget.DrawWindowBackground | QWidget.DrawChildren)))
                      PySide2.QtWidgets.QWidget.render(PySide2.QtGui.QPaintDevice, PySide2.QtCore.QPoint = Default(QPoint), PySide2.QtGui.QRegion = Default(QRegion), PySide2.QtWidgets.QWidget.RenderFlags = Instance(QWidget.RenderFlags(QWidget.DrawWindowBackground | QWidget.DrawChildren)))
                    Release of profile requested but WebEnginePage still not deleted. Expect troubles !
                    QPaintDevice: Cannot destroy paint device that is being painted
                    Segmentation fault (core dumped)
                    

                    type p == PySide2.QtGui.QPainter

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @fzet said in [PySide] How to save svg to png?:

                    PySide2.QtWidgets.QWidget.render(PySide2.QtGui.QPainter, PySide2.QtCore.QPoint, PySide2.QtGui.QRegion = Default(QRegion), PySide2.QtWidgets.QWidget.RenderFlags = Instance(QWidget.RenderFlags(QWidget.DrawWindowBackground | QWidget.DrawChildren)))

                    Seems to me that signature claims the QPoint does not default and is required? Or, pass p.device() for the other overload instead. If this all works.

                    F 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @fzet said in [PySide] How to save svg to png?:

                      PySide2.QtWidgets.QWidget.render(PySide2.QtGui.QPainter, PySide2.QtCore.QPoint, PySide2.QtGui.QRegion = Default(QRegion), PySide2.QtWidgets.QWidget.RenderFlags = Instance(QWidget.RenderFlags(QWidget.DrawWindowBackground | QWidget.DrawChildren)))

                      Seems to me that signature claims the QPoint does not default and is required? Or, pass p.device() for the other overload instead. If this all works.

                      F Offline
                      F Offline
                      fzet
                      wrote on last edited by fzet
                      #10

                      @JonB said in [PySide] How to save svg to png?:

                      Seems to me that signature claims the QPoint does not default and is required? Or, pass p.device() for the other overload instead. If this all works.

                      self.browser.page().view().render(p.device())
                      
                      QPainter::begin: A paint device can only be painted by one painter at a time.
                      QWidget::render: Cannot render with an inactive painter
                      

                      Maybe I need to do something else to render SVG from QtWebEngine to PNG? I dont know...

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved