Is it possible to make Inkscape autotrace PNG to SVG, but from the command line? - svg

I want to automate "raster to vector" conversions. PNG to SVG. (most Qs here on SO are the other way around)
I have tried the old command line tool autotrace on Linux, but I could not get it to run. I've tried to install a package, and to compile it from the source. Nope.
Then I've realised that Inkscape has "autotrace" now integrated in its codebase. I'd like to convert simple sketches from PNG to SVG.
And I want to do this in a Bash for-loop , with different autotrace settings (number of passes; ignore Speckles with max X pixels width) etc.
I've tried the "action" command-line option
inkscape --without-gui --actions="file-open:my.png"
and this brings up the small "png bitmap image import" dialog, waiting for me to confirm.
Also I've tried the verb command line option
inkscape --with-gui --verb="FileImport:my.png"
and this opens the large "Select file to import" dialog (ignoring my --verb argument)
At this point I gave up.
I want Inkscape to import a PNG picture, autotrace it with some settings, save it as SVG. Perhaps, beofre saving, duplicate the traced layer, lock the imported background layer, rename the layers from path-12345 to "tracesettings-x-y-z" etc.
(my final goal is to permute the tracing settings, to find good ones for my use-case, but that's not the focus of this question)

Inkscape is using potrace and autotrace to trace bitmap images into vector formats such as SVG and PDF.
Let's assume you have an image: foo.png that you want to trace to SVG using potrace:
First, you need to convert your image to a bitmap format (BMP).
Invoke the potrace command
# I am using ImageMagick convert command to convert PNG to BMP
convert foo.png foo.bmp
# Invoke potrace command with SVG backend
potrace -b svg foo.bmp
The result will be: foo.svg.

Related

How to crop a SVG image to the bounding box of the vector grafics elements

I have a SVG image here which is generated with 'empty space', i.e. only in the top-left corner is image content, whereas the rest is blank. I think it should be trivially possible to have an automated way to crop the image size to the bounding box of the objects - at least for some svg tooling like rsvg. However I am unable to find the 'command line trick' for this, etc.
I would like to do this on the command line (i.e. as part of a build script)
In principle I would be interested in a solution to the same problem but for pixel-based formats such as PNG as well.
rsvg does not have command line utilities for this problem, but Inkscape in its non-GUI mode has:
inkscape -o cropped.svg -D source.svg
will crop the file to the bounding box of all objects of the document. See the man page for a full documentation of the inkscape command line options. Especially note the --shell mode for batch processing multiple images.
For pixel-based formats there is the imagemagick -trim option:
convert source.png -trim +repage cropped.png

Opening an EPS file in Inkscape causes weird line artifacts

I'm trying to edit a vector graphics file from Freepik. The format is EPS and after installing both Inkscape and Ghostscript on Windows, I'm able to open the file with Inkscape. However, Inkscape introduces some weird artifacts (see lines and wrong colors in the picture below).
Side by side comparison, original vector (left) and SVG saved after opening the EPS file in Inkscape (right)
Is there a way to fix this issue?
It's a little difficult to tell, partly because this is a complex illustration and partly because the rendering is a little small. I'd suggest that the circular artefacts are caused by radial fills not being rendered completely.
This could simply be a rendering problem with Inkscape, or it could be that the radial fill has an Extend parameter which isn't being honoured. It could also be a problem calculating a clip.
It's not entirely obvious what you used to render the left hand image, is that Ghostscript ?
Generally I'd say this looks like an Inkscape bug and you should report it as such.
Edit
Reading through the Inkscape FAQ it seems that Inkscape uses SVG as its native format. That's going to mean that an awful lot of PostScript (and PDF) vector objects aren't going to be represented well. Shadings will either have to be rendered to an image or converted into a complex series of SVG primitives.
Following the link on 'How to open EPS files in Windows' from the FAQ suggests to me that EPS files are either rendered to an image or converted to PDF.
You could use Ghostscript to convert the EPS to PDF yourself, and then try loading the PDF into Inkscape to see if you get a better result. You can also open the PDF in, say, Acrobat to see if it looks OK there.
If the PDF looks fine in Acrobat, but not so good in Inkscape, then I'd say that's an Inkscape problem. If the PDF looks poor in Acrobat then that's a Ghostscript problem.
You can then report the problem as a bug to the appropriate site.
It seems that EPS has more capabilities than SVG and that's why some stuff looks weird when converted to PDF/SVG. Specifically, highlights in an EPS file are not properly rendered in an SVG file.
I checked the conversion from EPS to PDF via Ghostscript and the lines are already there, i.e. it's not an Inkscape bug.
Here's the original file to reproduce the problem:
https://www.freepik.com/free-vector/data-processing-factory-isometric-technology_8625296.htm
And here's what it looks like after converting it to PDF: The artifacts are not as noticeable on the PDF file, possibly because Ghostscript converts it with a higher DPI by default
My workaround to be able to edit the file (remove the background) was to:
open the EPS with Inkscape, ungroup the items
delete the background
export it as PNG
then use the PNG as a "mask" on GIMP to edit the JPG file that came together with the EPS.

Render an SVG with filter effects to PNG

I would like to render an SVG I've created to a hi-res (600 DPI) PNG. This SVG has filter effects, specifically gaussian blur.
Ideally rendering could be done via the command line.
I know the SVG is renderable because you can open it in Chrome/Chromium and the output I'd like to see is there.
Things I've tried:
Importing to Adobe Illustrator
svg2png
Imagemagick
Inkscape
Other command-line tools and programs I'm forgetting right now. Basically anything you could find with Google.
Current path I'm pursuing but stuck on:
Knowing that Chromium can render the SVG just fine and that it's open-source, I figured I'd download the library Chromium depends on to render SVGs (Skia, https://sites.google.com/site/skiadocs/) and use it to output a PNG. So far I've succeeded in installing Skia and running the very first example. I've found an example that renders an SVG (https://code.google.com/p/skia/source/browse/trunk/samplecode/SampleSVG.cpp?r=875) but haven't successfully run it on my OSX machine with the latest updates to XCode and command-line tools. A modified version of that example (with gyp file) that outputs a PNG would be amazing.
Other ideas that might work:
Getting pixel data from Chrome (how would I do that? A simple screenshot isn't enough because of the resolution of the image. I also want a scriptable, repeatable process.)
Using other graphics library that can render SVG. Maybe this comes from other web browsers, maybe not.
This seems to work nicely:
webkit2png -F blue.svg -o rendered
The -F says to generate the Full resolution PNG and no thumbnails, and the -o specified that the output filename will be rendered-full.png.
I have reduced the size so it fits on Stack Overflow, but the command shown actually gives the full-resolution 2880x2880 image.

ImageMagick convert SVG with text and external fonts to SVG with shapes (outlines) with no external fonts

I'm looking to to use ImageMagick to convert an SVG file with text and external fonts to another svg without text, instead have them be shapes or outlines.
Basically, can ImageMagick do the equivalent of Illustrator's "Create Outlines" functions?
I can currently convert the svg to png and all the text and fonts render correctly, but I'm just looking to remove need for the person opening the svg to have the correct fonts installed on their computer.
I believe that your installation of ImageMagick needs to be configured with the the "--with-autotrace" switch and Autotrace needs to be installed. Then a simple conversion command should work:
convert text.png vector.svg
I doubt ImageMagick will do it. What you need to do is load the SVG into an editor like Inkscape. Select the text and then convert it using Path->Object to Path.
Then you can save it and you can distribute it without worrying about the user not having the fonts.

ImageMagick convert SVG to PNG not working with RSVG enabled

I'm using ImageMagick's convert utility to convert SVG file to PNG image. At first, I used vanilla installation of IM on OSX (brew install imagemagick) to convert the SVG using:
$ convert file.svg file.png
This worked except that some of the image objects in that file were offset (actual links to images). I then read a related question that suggested ImageMagick to be compiled with rsvg support (homebrew does it with brew install imagemagick --use-rsvg).
Now, when I try to perform the conversion, no images are rendered. I tried using this SVG file, and the resulting PNG was blank. However, if any text exists on the SVG, it's rendered in the proper location. Any ideas how to proceed? thanks.
You should run this command to see a list of all 'delegates' your ImageMagick is trying to use:
convert -list delegate
To discover which delegate command ImageMagick uses for SVG processing, run
convert -list delegate | grep 'svg ='
You should see the binary + commandline parameters your convert tries to use. Im my case it is /opt/local/bin/rsvg-convert (but I'm not using Homebrew, I use MacPorts).
Now check if the binary is present at all on your system:
If yes, run it directly and debug from there.
If no, try to find where your Homebrew installation installed it, and change ImageMagick's configuration file for its delegates. It's called delegates.xml. MacPorts places it into /opt/local/etc/ImageMagick/delegates.xml -- I don't know where Homebrew stores it.
However, since your un-modified installation was already working, there must have been an SVG consuming delegate at work already then. Otherwise you would not have gotten any SVG processed at all.
This internal SVG rendering method of ImageMagick is called MSVG. This is far from being a feature-complete SVG interpreter/renderer.
Update:
To see what ImageMagick is doing for which format, run this command:
convert -list format
and for SVG run
convert -list format | grep SVG
Output on my system is:
MSVG SVG rw+ ImageMagick's own SVG internal renderer
SVG SVG rw+ Scalable Vector Graphics (RSVG 2.36.1)
SVGZ SVG rw+ Compressed Scalable Vector Graphics (RSVG 2.36.1)
After you installed rsvg, the internal method to render SVGs will not have gone away. You can still force ImageMagick to use the internal renderer by adding MSVG: to the commandline like this:
convert MSVG:file.svg file.png
Just for completeness' sake...
Oh, I now had a look at the SVG file you linked to.
It contains JavaScript. I do not think that the RSVGlib does support JavaScript inside SVGs. I know for sure that the internal SVG coder named MSVG does not.
I had a similar problem and was just told to install inkscape and it works,
ImageMagick converts svg graph into pdf with tilted x-axis labels, any ideas?
The delegate list did not change but the output did. On further fiddling with forced MSVG I could not get the problem to go away. So another delegate is probably the best solution.

Resources