Extract contour of imageinto svg

380 Views Asked by At

I have png-images of a grayscale circle and background alpha enter image description here

I would like to extract the 50% contour (only the diagonal line) and export it as a path into an svg-image. Until now I tried to do this with ImageMagick and Potrace, but I was not able to get the desired result:

  1. The following command gives me the complete area, but not the border. After opening it in Inkskape I can by hand make the contour transparent and add a color to the border and maybe afterwards also delete the parts of the circle which I don't want to have. But I would rather have something which runs automatically.

    convert input.png -threshold 50% pgm:- | potrace - --svg --invert > result.svg

  2. I can extract only the border first with ImageMagick, but also a part of the circle. I think I would be able to maybe put a mask on the result to delete the circle again, but then if I try to use potrace to generate an svg-image, I can only get a shape of the form of the contour, but not a path.

    convert input.png -threshold 50% -edge 1 pgm:- | potrace - --svg --invert > result.svg

Maybe there is a completely different way to achieve what I want to have. It would be nice to do this by standard tools from the command line and not by hand or by writing a script to do it completely from scratch.

1

There are 1 best solutions below

3
On

Here is how to extract your 50% line in Imagemagick. (To convert to SVG, see https://imagemagick.org/Usage/draw/#svg_output)

enter image description here

convert circle.png -fuzz 1% -fill red -opaque "gray(50%)" -fuzz 0  -fill black +opaque red -fill white -opaque red -alpha off result.png

enter image description here

If you want just the coordinates, then in Unix syntax

convert circle.png -fuzz 1% -fill red -opaque "gray(50%)" -fuzz 0  -fill black +opaque red -fill white -opaque red -alpha off txt: | sed 's/^\(.*\):.*$/\1/g' | tail -n +2

You may want to reduce the fuzz value to get a narrower line.