I was investigating how to output SVG files with Python, but Qt (PySide) is easier than python's strongest SVG library svgwrite. Then I came to the conclusion that it might be the strongest (I admit the objection).
I don't think most people know SVG, but I'll explain it for the time being.
SVG is one of the typical vector image formats. Compared to the conventional raster format image, there is no jag even if it is scaled up or down, so a beautiful display can be achieved even in places where the display size fluctuates depending on the environment such as a web browser.
For SVG output using Qt (PySide), use QSvgGenerator. Let's take a look at a simple sample that outputs SVG with PySide.
#! usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function, absolute_import
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSvg import *
app = QApplication(sys.argv)
svg_gen = QSvgGenerator()
svg_gen.setFileName("hello.svg")
svg_gen.setSize(QSize(200, 80))
svg_gen.setViewBox(QRect(0, 0, 200, 80))
svg_gen.setTitle("hello svg")
svg_gen.setDescription("this is sample svg.")
painter = QPainter()
painter.begin(svg_gen)
rect = QRect(0, 0, 200, 80)
painter.fillRect(rect, Qt.yellow)
painter.setPen(Qt.blue)
painter.setFont(QFont("Arial", 30))
painter.drawText(rect, Qt.AlignCenter, "Hello SVG")
painter.end()
Display result (* Displayed in PNG because SVG cannot be pasted)
Please note that only SVG related modules are imported with `from PySide.QtSvg import *`
.
All you have to do is enter the SVG settings into the QSvg Generator and then use QPainter to draw as usual. I haven't manipulated the elements of SVG at all, but this alone creates an SVG file. (Of course, it's not a complete SVG conversion, features that SVG doesn't have, such as text wrap, are ignored)
By the way, SVG drawing can also be displayed in QGraphicsView with QSvgWidget or QGraphicsSvgItem.
SVG output is also possible from QGraphicsScene.
scene = QGraphicsScene()
rect = QRect(0, 0, 200, 80)
scene.setSceneRect(rect)
scene.addRect(rect, QPen(Qt.transparent), QBrush(Qt.yellow))
text_item = scene.addText("Hello SVG", QFont("Arial", 30))
text_item.setDefaultTextColor(Qt.blue)
painter = QPainter()
painter.begin(svg_gen)
scene.render(painter)
painter.end()
It is big that QGraphicsScene can be output, you can adjust the item position with the mouse etc. with QGraphicsView and output it to SVG as it is. You can easily create an SVG live preview environment.
svgwrite is a flexible and very easy-to-use library. However, I would like to mention the superiority over svgwrite compared to the case of using Qt.
Since svgwrite is a wrapper for XML, for better or for worse, there is no `` `getBBox``` that asks for a bounty box when displayed. This is very troublesome when dealing with text because you do not know the size of the text.
Since it outputs SVG, it is natural that knowledge about elements is required, but with Qt you can output SVG without any knowledge about elements.
When outputting SVG on the program, try & error is required. If you use Qt, the drawing of QPainter becomes SVG as it is, so the preview environment is inevitably completed. Even after converting to SVG, you can check it with QSvgWidget, so once you have built a program, you can modify and create it at the same time with live preview.
Note that the SVG version 1.2 that Qt5 can handle, but the SVG version is 1.1 because it is Qt 4.8 when using PySide. I just hope that PySide 2 will be officially released as soon as possible.
Outputting SVG using Qt is very easy. If you know Qt, you don't need to know much about SVG, which is a big advantage for SVG beginners.
Creating flexible SVG such as linking with HTML5 and applying style sheets has to be handed over to svgwrite, but Qt is a big option for display-only SVG.
By the way, I automatically analyze the structure of C language and output the block diagram (Is it the name?) In SVG. The following figure is output for the BITMAPINFOHEADER structure.
It is convenient to embed it in a manual such as Sphinx by automatic generation because you can understand the structure such as putting.
Recommended Posts