Using reportlab to build PDF with vector-based graphs generated by matplotlib - svg

I'm trying to build PDF-documents on the server-side in a Django-Installation using reportlab. These documents should contain several graphs which are to be created with matplotlib.
I already figured out how to make reportlab use matplotlib's images without dumping them to the filesystem temporarily by passing PIL-Image objects directly to the Image()-flowable. This works surprisingly well for rasterized images formats like PNG.
Now, the icing on the cake would be able to embed vector based graphics (like SVG).
I used svglib to convert SVGs generated by matplotlib to reportlab graphic objects but unfortunately svglib does omit the tickmarks and axis labels. On some graphs it fails in general.
Do you have any ideas?

This page has a solution that I haven't had a chance to test myself yet: https://web.archive.org/web/20120725125858/http://lateral.netmanagers.com.ar/weblog/posts/BB753.html

You can generate matplotlib graphics as pdf and use pdfrw to embed it in reportlab canvas as described in this answer

Related

Converting sequence of SVG images (which contain Emojis) to a MPEG video

I googled and even asked chatGPT but I'm unable to find a solution and hope to get some guidance here.
First I've to mention that I'm not a programmer but rather a beginner.
Following a short description of what I'm trying to achive and what I've done so far.
I gather data and create a circular visualization using Circos which produces SVG
and PNG images.
(unfortunately the PNG doesn't give me the option of searching for
text an make replaecments), nevertheless I can use them to sucessfuly produce a
MPEG movie using FFmpeg. Therefore I need to use the SVG output to apply the
desired changes.
So I tried to use CairoSVG to render the SVG file to a PNG image but it does not
render emojis by default because the are not part of the SVG specification and
CairoSVG only supports features defined in the SVG specification. The Emojis are
stored as Unicode characters and are not natively supported in SVG
Next I tried to use PIL (Python Imaging Library) as it provides support for Unicode
characters, including emojis, when converting images to and from various formats.
Unfortunately PIL does not have native support for converting SVG files to PNG and
it seems that PIL is primarily designed for creating and manipulating images in a
variety of formats, but does not have built-in support for reading or converting
SVG files.
So now my questions are:
Would FFmpeg give me the desired results, if I compile it using the --enable-
librsvg option so it can convert a sequence of SVG images to a video but i'm not
sure if it supports emojis rendered correctly and want to spare me the hassle as
I'm pretty sure to struggle compiling it on my Mac running Ventura?
Are the maybe other ways or posibilities to solve that problem?
Many thanks in advance for your help or any hint :-)
Have all a nice weekend and take care
Regards,
Deekee
NB: an example of the circular visualization can be found here animated graph and the static version annotated graph
Problem solved, I used the html2image Python module which converts the SVG (including embedded Emoji's) nicely to a PNG image an then use those images to create a MPG4 video using FFmpeg.

How can you render an SVG to a png in a specific size (python)

I am Working on a small Image comparing script where the reference images are generated as SVGs and the compare images are PNGs.
I can transform the SVG files to PNG (using svglib and renderpm) but canĀ“t specify the size I want them to be generated as(renderscale seems to cut of a part of the picture), but I need to get them to the same size for the compare functions and resizing the pngs nullifyes the whole purpose of vector graphics in itself. Any Ideas?
Regards a python noob

How to have a background image in Altair Python

I'm trying to use Altair to have a dynamic map, I can easily do the dynamic part, however I am trying to add an image as background of the plot (bitmap image), I cannot find any functions in this purpose and every example on the Altair documentation is based on Url/Json feature. Does anyone have already done this kind of plot ? Is it possible to maybe combine a matplotlib with the background image and a dynamic part such as in Altair ?
Thanks Everyone,
Antoine.
It is not possible to embed a bitmap image within Altair. Vega supports an image mark, but that has not yet made its way into Vega-Lite, which drives the API of Altair.
Requests for this feature in Vega-Lite can be tracked in this GitHub Issue.

How to get Numpy array of Canvas data?

I am building an application with Tkinter, where one is able to draw e.g. lines in a Canvas. This works well. However, I'm unable to find a method for getting the current Canvas data. Preferably I would like to get a numpy array out of the current Canvas data, since my post-processing steps are mostly using numpy.
Is there any way to build numpy arrays out of the Canvas data? In some color format like RGB, by preference?
I know that I can get the information e.g. of lines (like coordinates) out of the Canvas, but I do not need this information. I need a rasterized image data of the whole Canvas scene. Like a numpy array or a (rasterized) image (jpg, png, tiff, bitmap, ...).
Like #Bryan Oakley said: there is no way to get a rasterized version of a Tkinter Canvas drawing.
However, I figured out this workaround:
import skimage.io as ski_io
(...)
# draw your canvas
(...)
# save canvas to .eps (postscript) file
canvas.postscript(file="tmp_canvas.eps",
colormode="color",
width=CANVAS_WIDTH,
height=CANVAS_HEIGHT,
pagewidth=CANVAS_WIDTH-1,
pageheight=CANVAS_HEIGHT-1)
# read the postscript data
data = ski_io.imread("tmp_canvas.eps")
# write a rasterized png file
ski_io.imsave("canvas_image.png", data)
I do not really like workarounds, but skimage seems to be the fastest solution for reading postscript files and writing pngs.
Scikit-image is developed as a toolkit for SciPy, therefore it is working with scipy.ndimage internally, which is exactly what I want and can be used to create np.ndarray very easily.
Additionally scikit-learn is a powerful and fast image processing software itself, which can manipulate, read, and save various image formats.
Now you have the full choice: get a NumPy np.ndarray out of Canvas data for further computations, manipulate the scipy.ndimage with SciPy/scikit-image or save the data, e.g. as a rasterized png, to disk.

Save a drawing made by OCaml graphics in a EPS and PDF file

I made an OCaml program that draws L-systems using the turtle interpretation.
What I'm looking for is to save what I draw with graphics to an image of EPS and PDF file.
But I never done this before and I don't know how to do, so I've looked in graphics librairy and all I've found is get_image to get an Image file and dump_image to get an matrix color from an Image file but from here I don't know how to save the data into a EPS or PDF file.
Does anybody have an idea about how to do that?
I never used it, but there is graphicspdf which implements the Graphics interface but outputs to PDF (opam install graphicspdf). Similar for postscript is GraphPS (not packaged in opam).
Alternatively if you program is well designed you should be able to render to multiple backends. In that case Vg allows you to render to PDF, SVG or the html canvas (opam install vg). There is also ocaml-cairo that provides you bindings to the C library libcairo and allows you to render to multiple rendering backends (opam install cairo).

Resources