19
$\begingroup$

In the document I found that SVG files can be exported, but there is no information of Import. How can I do that?

$\endgroup$
3
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, [by clicking the checkmark sign](tinyurl.com/4srwe26 $\endgroup$
    – Dunlop
    Commented Jan 7, 2020 at 5:37
  • 3
    $\begingroup$ If you look at the documentation (I am using MMA 12) SVG is not listed as a format that can be imported. You can check which file formats are supported by using the command $ImportFormats $\endgroup$
    – Dunlop
    Commented Jan 7, 2020 at 5:38
  • 1
    $\begingroup$ @Dunlop Thank you for your advice. I just want to know how can I do it, it's not limited to be done with Import. I didn't found any related information on this forum, so I think maybe I have to use some external programs to do this. Or I may need to learn something about svg and do it by myself. $\endgroup$
    – bcegkmqs23
    Commented Jan 7, 2020 at 7:50

4 Answers 4

22
$\begingroup$

This is not currently supported by the built-in functions, but there is a function called SVGImport in the function repository that can do this: https://resources.wolframcloud.com/FunctionRepository/resources/SVGImport

$\endgroup$
1
  • $\begingroup$ Thank you. I've forgotten this site. I knew this website just yesterday. $\endgroup$
    – bcegkmqs23
    Commented Jan 7, 2020 at 8:12
7
$\begingroup$

Alternative answer: Use the SVG importer from my Prototypes paclet:

https://github.com/arnoudbuzing/prototypes/releases/tag/0.5.6

To install the paclet, run this command:

PacletInstall["https://github.com/arnoudbuzing/prototypes/releases/download/v0.5.6/Prototypes-0.5.6.paclet"]

Note that this paclet has many many functions, but you may be able to extract just the SVG importer if that's all you need.

After that you can load the package:

Get["Prototypes`"]

Normally this package autoloads when you use a function from the package, but not in this case which uses Import.

Next run Import to import the SVG file (example source):

Import["D:\\tmp\\in.svg", "SVG"]

enter image description here

Important note: This implementation uses the Apache-Batik java library and imports the SVG as a raster image and not as a graphics object. So you lose the scalability of the SVG. On the other hand, this implementation does a very faithful rendering of SVG file (exceeding, in certain cases, the result from the SVGImport resource function (which works well in a lot of cases too).

$\endgroup$
3
  • 1
    $\begingroup$ When is SVG support coming by default in WL? 12.3? 13.0? Are you guys working on it? $\endgroup$
    – user13892
    Commented Apr 7, 2021 at 23:04
  • $\begingroup$ Not sure yet when this will be in WL, because fully supporting it is complex. There is now also this WFR entry though: resources.wolframcloud.com/FunctionRepository/resources/… $\endgroup$ Commented Apr 27, 2021 at 19:25
  • $\begingroup$ Wait, does this mean that Java has to be present for it to work? $\endgroup$
    – Michael
    Commented Sep 8, 2021 at 22:59
4
$\begingroup$

Recent versions do a better job at Importing. Here are two methods:

marianne="https://upload.wikimedia.org/wikipedia/en/d/d1/2024_Summer_Olympics_logo.svg";
ResourceFunction["SVGImport"][marianne] 
Import[marianne, "Graphics"]

These will import a filled curve, equivalent to a Bezier curve. A Marianne Community Post goes into more detail.

$\endgroup$
2
  • 1
    $\begingroup$ This is the same as the accepted answer by C.E., right? :) $\endgroup$
    – Domen
    Commented Jun 12 at 14:45
  • $\begingroup$ Import[marianne, "Graphics"] is new, but does the same thing. $\endgroup$
    – Ed Pegg
    Commented Jun 12 at 19:37
2
$\begingroup$

Depending on your usage scenario, converting the SVG files to PDF via Inkscape and then importing the PDF files using the regular Mathematica Import[] function might be a viable alternative.

I tried this with Inkscape 0.92 and Mathematica 12 on Linux, albeit only for one file.

Inkscape can also do the conversion from SVG to PDF using command line options. Therefore, if you have many files, you could automatize the conversion process.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.