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

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

Related

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

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.

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.

imagemagick.net embedded fonts

Does anyone have experience or insights into how to convert SVG to PNG using ImageMagick.NET where the SVG has custom embedded fonts?
I've seen similar posts about this, but I'm specifically interested in ImageMagick.net and I would like to avoid calling anything from command line. I currently use Batik, but I want to move this to Azure and I don't want to go through starting the JVM so that the JAR file can be executed.
Note additionally, my problem is that I am getting the SVG from another party and most fonts are not freely available, so I can't just pre-install all potential fonts.
Many thanks for any insight.
I don't know anything about azure platform, but I can share some insight on the SVG process within ImageMagick. This is more of a long-winded comment than an answer
ImageMagick (of which Magick.net sets on top of) performs tasks on raster images, so any vector input must be "decoded" into a raster image before any additional work be be done. For SVG images, I'm aware of three ways ImageMagick can render vector graphics into authenticated pixels.
Use a primitive internal MSVG coder to draw each shape.
Pass the rendering work to librsvg if compiled with delegate library support. (Usually on *NIX systems)
Call an external command-line application. Identical to the suggestions referenced in the links you provided.
If I would map it out, I would imagine it would look something like this.
SVG Fonts with MSVG (option #1)
I believe this is the option your asking about as it would be the default configuration for must ImageMagick installs. For a font to be rendered, the typeface must be found on the system, and supported by freetype (.ttf or .otf files). Embedded fonts are usually base64 ttf files attached inline under the #font-face CSS at-rule. If this is true, you should be able to preprocess the document, extract the font-file to a local/temporary filesystem, and assign it with MagickReadSettings.FontFamily before reading the SVG document. Although I'm not sure if this would work with multiple fonts.
SVG Fonts with RSVG (option #2)
The librsvg offers a lot more support for SVG specs then the internal renderer, but also enforces more restricted approach for external resources. There's also an open issue with adding support to #font-face, so you might be forced to do option #1 anyway.
External Command-line (option #3)
This would be your best option. ImageMagick's delegate.xml file can be altered to call other utilities. Inkscape, for example, can be called by ImageMagick with the following rule..
<delegate decode="svg" command="inkscape.exe -e %o %i"/>
Although I'm not sure if inkscape is a good example as support for CSS fonts are still listed on a wishlist.
TL;DR
... convert SVG to PNG using ImageMagick.NET where the SVG has custom embedded fonts?
All-n-all, it boils down to the right tool for the right job. If your only use-case is converting SVG+CSS to PNG, and you have no additional raster manipulation tasks, a direct utility like batik is more appropriate than ImageMagick. Like it or not, installing a JRE to execute a JAR file might be the best option. Magick.net by itself doesn't meet your requirements.

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