Generating svg images using convert "command line"

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ahmed.eldawy

Generating svg images using convert "command line"

Post by ahmed.eldawy »

Hello,
I'm using ImageMagick command line tool (convert) to generate svg images.
First, I tried this command to generate an image with two circle
convert -size 100x100 -draw 'circle 20,20 10,10' -draw 'circle 30,30 50,50' xc: test.svg
It worked correctly but it generated the last circle only
I changed it to
convert -size 100x100 -draw 'circle 20,20 10,10 circle 30,30 50,50' xc: test.svg
and it generated the two circles correctly. However, it didn't respect any of the attributes like fill and stroke. The resulted circles were always black and solid.
My questions are
1) Do I really have to write all my drawing commands in one draw argument (like the second example)?
2) Can I use graphics settings like stroke, strokewidth and fill to draw svg images? how?
3) Can I use DIFFERENT colors for the same svg image? how?

I'm running this version
Version: ImageMagick 6.5.4-2 2009-07-07 Q8 OpenMP http://www.imagemagick.org
Copyright: Copyright (C) 1999-2009 ImageMagick Studio LLC
Which is compiled from source on my Ubuntu 9.04 machine.

Thanks in advance.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Generating svg images using convert "command line"

Post by Bonzo »

This will work for png but not for svg - you just get one black circle!

Code: Select all

convert -size 200x200 xc:none -fill red -draw "circle 20,20 10,10" -stroke black -strokewidth 10 -fill blue -draw "circle 100,100 50,50"  test.png
With this you get the two circles but they are both black.

Code: Select all

convert -size 200x200 xc:none -fill red -draw "circle 20,20 10,10 circle 100,100 50,50"  test.svg
From the ImageMagick website:
SVG RW Scalable Vector Graphics Requires libxml2 and freetype-2. Note that SVG is a complex specification and support for the specification in ImageMagick is not complete.
From Anthonys examples: http://www.imagemagick.org/Usage/draw/#svg
ahmed.eldawy

Re: Generating svg images using convert "command line"

Post by ahmed.eldawy »

Thanks Bonzo for your reply. This was really helpful.
Do you know another tool that can generate svg images?
I need to generate svg images from ns2 nam trace files.
Simply I have a list of x,y coordinates and I need to generate some vector images from them like:
drawing circles at node points
draw lines between nearby points
draw coverage circles (centered at node points and of radius r)
...
and other simple images. Colors are important because they distinguish working Vs non-working nodes and active Vs non-active links.
I can generate png files with success but I need to make a high quality report supported with vector images.
Thanks a lot for your efforts
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Generating svg images using convert "command line"

Post by magick »

Try inkscape.
ahmed.eldawy

Re: Generating svg images using convert "command line"

Post by ahmed.eldawy »

I use inkscape and it has a very user friendly interface.
What I need is an API to draw vector images. I thought that ImageMagick would help me with this as it has a very powerful and simple API. I can't draw 500 images each having 100 circles at certain locations (x,y) using the mouse. However, I can make a ruby script that does the job.
Any ideas?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Generating svg images using convert "command line"

Post by fmw42 »

not an expert, but did you install RSVG and freetype delegates.

see http://www.imagemagick.org/Usage/draw/
http://www.imagemagick.org/Usage/draw/#svg

hopefully Anthony can answer most of your questions once he gets online
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating svg images using convert "command line"

Post by anthony »

IM is a raster (pixel array) image processor, and as such when the IM draw command is used
it 'draws' on a raster image. It does NOT generate SVG vectors!

When you ask IM to generate a SVG version of an image it does so by calling the AutoTrace library. This is a bitmap to vector graphic converter, but it has some limitations.
  • First it does NOT handle anti-aliased pixels very well. You are better off thresholding the image so as to brovide a clean edge for AutoTrace to trace other regionas and shapes of color.
  • Second it will only generate complex polygons of the shape. If you ask for a circle you will NOT get a circle, but a rough polygonal representation of a circle, appropriate for the scale of the image being converted.
  • Third it has to make decisions about weathe rto make a smooth curve or a sharp point when the direction of a shapes edge changes. There are many rules and options for this which generally work well. However in some situations I have found a need to 'tweek' the 'angle' option to make
    a sharp change instead of a curve for some images.
As you can see I have experimented with Raster to SVG conversion using Autotrace. I even used it as trial method of image segmentation...


Compare the results of the scripts "segment_image" and "segment_via_svg" in the IM Examples Scripts area http://www.imagemagick.org/Usage/scripts/ using the image http://www.imagemagick.org/Usage/images/segment.png
Image

This makes a good example of how good (or bad) the raster to SVG conversion is...

segment_image
Image


segment_via_svg
Image
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ahmed.eldawy

Re: Generating svg images using convert "command line"

Post by ahmed.eldawy »

Thanks all for your help.
For now I'll use ImageMagick to generate raster images.
Meanwhile, I'll try to search/develop libraries that allow generating svg images. I found that I can write plugins for InkScape in Python. But I search for simpler APIs like RMagick http://rmagick.rubyforge.org/ and QuickMagick http://quickmagick.rubyforge.org/quick_ ... index.html.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating svg images using convert "command line"

Post by anthony »

One method I used a VERY long time ago (almost 20 years now but still valid) was to output 'fig' files. The fig vector language used for the 'xfig' is very simple and easy to program. You can then convert that to pdf or svg directly using 'transfig'.

Hmmm oops sorry svg is not available (too new for this old application) :-(
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Generating svg images using convert "command line"

Post by Bonzo »

Lateral thinking - generate the png with Imagemagick and then convert that to svg with Inkscape or similar ?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating svg images using convert "command line"

Post by anthony »

SVG does not accept a raster image. it is pure vector.

The autotrace (SVG output from ImageMagick) is the PNG to SVG conversion
and that is essentially what the user was doing, not what he is wanting.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
cancerberosgx
Posts: 10
Joined: 2019-07-02T18:53:00-07:00
Authentication code: 1152

Re: Generating svg images using convert "command line"

Post by cancerberosgx »

I'm using ImageMagick command line tool (convert) to generate svg images.
Why not write the paths in a svg document and then rasterize it to a bitmap (which is safe) ? why do you need to use IM to draw vectors ?

This will work for png but not for svg - you just get one black circle!
Did you try using -compose or generating several "files" one for each shape using +clone between parenthesis ? perhaps it will end up joining all shapes in a final one ? (really I have no-idea)

When you ask IM to generate a SVG version of an image it does so by calling the AutoTrace library.
I was playing with image tracing technologies recently (JavaScript). I'm joining different tracer implementations I've found (so far only 2 in js) in this project:

browser/node.js API: https://www.npmjs.com/package/svg-png-converter
command line tool https://www.npmjs.com/package/svg-png-converter-cli

user-friendly playground (WIP) https://cancerberosgx.github.io/demos/b ... converter/
no so friendly playground (only potrace implementation) https://cancerberosgx.github.io/demos/s ... layground/#

If you play a little bit, for logos/drawing it works nice (and if you only need monochrome output then potrace implementation is almost perfect).

However if you want to "vectorize" photos the story is different. You can archieve almost perfect replicas, but the svg will have enourmous anount of shapes without any semantics and for most vector graphic use cases this is unuseful (imagine a plotting machine trying to cut/plot).

Also I recently discovered "geometrize/primitive" technologies (https://www.geometrize.co.uk/, https://github.com/fogleman/primitive) and made a port to JavaScript node.js browser API and CLI https://www.npmjs.com/package/geometrizejs to easily test it. Although this technology was not designed for this (mostly to create art/animations/effects) I realized that it's also capable of trace an image.

Finally I'm using svgo (svg optimizer in node.js and browser) which is capable of simplify any svg to a single or few paths. This seems interesting to combine with ImageMagick but this time as a rasterizer tool and compare with other similar technologies. BTW A fwe days ago I started working on a Node.js and browser API and CLI for ImageMagick utilities command line tools here https://www.npmjs.com/package/magica (it works fine, although slow compared to the real thing, same api for desktop and browser)

Since now I have all these supported in the same language and platforms one crazy objective would be to build (really don't know how) something that convolves a bitmap - edge detection and somehow find out the simpler paths to draw its main objects. I think I have a formal definition of these "main objects" but my math and expertise in areas like machine learning (that could probably be helpful in this) are very poor.

Do you know anybody worked on something like that bitmap->edges->"shapes"->vectors ?. or mixing imagemagick data extraction commands with machine learning to detect / separate / classigicate objects ? or some paper regarding important image properties to consider in this regard . The objective would be to generate arbitrary-simple vectors representing "shapes" and edges from arbitrary bitmap. The definition of these "objects", to start, could be something like clusters of similar color pixels... well I would really appreciate any tip or link on any similar, thanks. I didn't see any technology approaching the problem like this, but probably there are many.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Generating svg images using convert "command line"

Post by anthony »

cancerberosgx For converting shapes to vectors, you may like to look at IM Examples...

http://www.imagemagick.org/Usage//draw/#svg_output

Where I go though the process, using autotrace.

however SVG is not really suitable for 'drawing' real photos. I have now added a link from that area to your post as you provide some good links to various programs and techniques.

As for your goal. I myself can not help you a lot. Do you have an example of what you have achieved?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Generating svg images using convert "command line"

Post by snibgo »

cancerberosgx wrote:Do you know anybody worked on something like that bitmap->edges->"shapes"->vectors ?
For some work I did vectorizing cartoons (with areas of flat colour), potrace and autotrace couldn't do what I needed, so I started from scratch. See Lines, points and curves.

Photos aren't cartoons, but can be made so. See Flat-colour cartoons.
snibgo's IM pages: im.snibgo.com
Post Reply