[PYTHON] Isn't Qt the strongest library if you want to easily output SVG?

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).

What is SVG?

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.

SVG output using Qt

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) g5039.png

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 from QGraphicsScene (2017-01-05 postscript)

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.

Advantage over svgwrite

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.

svgwrite does not have getBBox

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.

No knowledge of SVG elements required

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.

Easy preview of the creation process

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.

About SVG version

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.

Summary

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.

How to use SVG automatic generation

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.

g4863.png

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

Isn't Qt the strongest library if you want to easily output SVG?
If you want your colleagues to use the same language
I want to output to the console coolly
If you want to include awsebcli with CircleCI, specify the python version
AssertNumQueries is useful if you want to easily test N + 1 queries with django
If you want to create a Word Cloud.
When you want to update the chrome driver.
[Note] What to do if the Qt library conflicts between pyqt and opencv
If you want to enter the virtual environment with jupyter, nb_conda_kernels is recommended
If you want to use Cython, also include python-dev
The programming language you want to be able to use
If you want to put an argument in the closure function and execute it later
I want to initialize if the value is empty (python)
[Python] If you suddenly want to create an inquiry form
Keras I want to get the output of any layer !!
[Django] What to do if the model you want to create has a large number of fields
When you want to use multiple versions of the same Python library (virtual environment using venv)
[Django] Carefully explain the escape route if you really want to use the table of another application
If you want to become a data scientist, start with Kaggle
Don't write Python if you want to speed it up with Python
If you remove the list to be looped, you will get terrible.
When you want to keep the Sphinx documentation theme as usual
I want to output the beginning of the next month with Python
When you want to print to standard output with print while testing with pytest
If you want to assign csv export to a variable in python
When you want to save the result of the callback function somewhere
[TensorFlow] If you want to run TensorBoard, install it with pip
What to do if you can't use the trash in Lubuntu 18.04.
How to find if you don't know the Java installation directory
When you want to adjust the axis scale interval with APLpy
If you just want to get the dump file of the server, it was convenient to build an http server
If you want to switch the execution user in the middle of a Fabric task, settings context manager