Mobile app version of vmapp.org
Login or Join
Annie732

: Convert SVG to image with multiple fonts I have an SVG with a custom font defined in the defs section. When I load it up in Safari everything's fine. Unfortunately, when I try to convert it,

@Annie732

Posted in: #FileConversion #Fonts #Python #Svg

I have an SVG with a custom font defined in the defs section.
When I load it up in Safari everything's fine. Unfortunately, when I try to convert
it, with Python and ImageMagick for example, to a png the information about the
font gets lost.

I'd like to know, if it is possible (and if yes how :D) to convert that image and retain the font in the resulting image... preferably using python.

For example:
Imagine you have an SVG that describes a musical sheet and a special font is used for the notes. So there is a defs section, which describes different font-families for different purposes.

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="productSvg" class="playbackCanvas" style="width: 582px; height: 757px;" viewBox="0 0 13285.3 17280">
<defs>
<style type="text/css">
@font -face {
font-family: 'MyFont';
src: url('MyFont-Normal.otf');
}
@font -face {
font-family: 'Helvetica';
...
}
</style>
</defs>
<text font-size="95" font-family="Helvetica" x="2" y="2" id="abc" style="fill: rgb(0, 0, 0); stroke-width: 0;">x</text>
<text font-size="120" font-family="MyFont" x="6035" y="2681" style="fill: rgb(0, 0, 0); stroke-width: 0;">4 fr</text>
...


No problem in a browser. At least in Safari. It did not work as well in Chrome in my tests.

Anyways, I found a way to convert the SVG with a custom font using ImageMagick.

convert -font "MyFont-Normal" example.svg example.png


Unfortunately, I can't pass multiple fonts as Arguments. Therefore, the text with the Helvetica font-family will also have the special font.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Annie732

1 Comments

Sorted by latest first Latest Oldest Best

 

@Annie732

This one is for @PieBie . So this is how I solved the problem. As indicated in the comments, it is possible to call inkscape from the command line and
the command can also be called from python. The following steps were necessary to make it work on my machine.


Install inkscape
If the svg includes custom fonts make sure that inkscape has access to them. I'm on mac so I had to install my custom fonts to /Library/Fonts
Use the -e option to export your image


In my case the command looked like this:

inkscape /path/to/svg -e ~/blub.png -w 1000 -h 1300


I added the -w and -h options, because my png turned out to be huge without the constraints.

In python it might look something like the following

import subprocess
#Some other code that creates the SVG or scrapes it from the internet might be here

subprocess.Popen(["inkscape", "/path/to/svg", "-e", "~/blub.png", "-w", "1000", "-h", "1300"])

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme