[PySide] How to save svg to png?
-
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)
-
Hi,
As stated in the documentation, QtSvg support only the static features of SVG 1.2 tiny.
-
@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 ?
-
QtWebEngine is a web browser that implements the whole specification. From a cursorary look, QtSvg does not handle font-face.
-
@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
-
@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, passp.device()
for the other overload instead. If this all works. -
@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...