Menu

Getting SVG paths for all faces

Help
Jan Benes
2018-04-16
2018-05-29
  • Jan Benes

    Jan Benes - 2018-04-16

    Hi,

    let's assume I have an image consisting of three vertical bars (like, say, the French flag) that are colored white (left), black (middle) and white (right).

    If I trace this as SVG, I get one black path for the middle stripe, while the background is defined implicitly (as far as I can tell). I would like to get both of those as separate "white" paths. In other words, is there a way, for a two colored input image, to get two sets of mutually exclusive (let's ignore https://sourceforge.net/p/potrace/discussion/300716/thread/55e01763/ for now) paths that cover the whole of the original image and that are continous when drawn (for the french flag example, I'd get 3 separate paths)? Is there a way to achieve that?

    I've tried
    1. using the --opaque switch, which seems to only work for holes, not borders
    2. adding a large enough inverted border to the image, so that I force the algorithm to create these shapes. However, the blackparts of this border are still a part of other shapes and I cannot easily remove them. I think I could remove them with some further computation, but I also worry abou how they influence the corner shapes where the original border was.
    3. I could add a background (white) color border and then another ink (black) border, which together with --opaque could generate all I need, except I'd also have to trim the extra white border from the resulting svg.

    Any suggestions/tricks very much appreciated!

    Thanks

     
  • Peter Selinger

    Peter Selinger - 2018-04-17

    Hi Jan,

    that's an interesting question. As you have noticed, Potrace really only cares about the black parts; it considers white to be background. With --opaque, only interior background regions will be colored in.

    There is not easy solution within Potrace itself, as Potrace is not currently tailored to handle 3-way intersections (which you get where a black/white border hits the outside border of the image).

    One external solution is to load the black path (without --opaque) into SVG, create a white rectangle in SVG, and subtract the black path from the white rectangle. This way you get the complement as a separate shape.

    -- Peter

     
  • Jan Benes

    Jan Benes - 2018-05-29

    Hi Peter,

    thanks for clearing this up for me (and sorry for the delay in answering). After also reading the library documentation (which, by the way, is great), I think I understand better why potrace can't really do what I need.

    As for your suggestion, I'm going to assume that by subtracting, you mean <mask> out the black from the white background. The problem with that (for me) would be that I would get the result as one single path (even if it is discontinuous), whereas I'd really need to have each component separately.

    In the meantime, I've taken a somewhat different approach. Roughly, I use --opaque to get the inside shapes and then try to detect whether the rest of the background is just one component (if all of the border pixels are white, I assume it is), in which case I can be sure the background is one piece only and using masks, I can create the one missing background component. If the background is possibly split into more than one component (e.g. a B&W french flag), I give up and fail. For now, this is good enough for me.

    Another way to do this would be to detect where the black components touch the image border, walk them away from the image border until we get to another image border point, and then walk along the image border to fill in the missing segments (for my use case, this is good enough; for perfect split into black and white components, child paths would also have to handled). This might also put some constraints on the turn policy, it's been a while since I looked into this. Anyway, I think this is too much of a hassle for a use-case few need and it's been a while since I did C/C++, so I'm just leaving this idea here for anyone who comes across this.

    Thanks for the effort you put into potrace!

    PS: in potracelib.pdf, 2.3.1., you say "area is below the given threshold", but then, in Figure 7, turdsize=3 removes a 3-pixel path. I think it should read "below or equal".

     

    Last edit: Jan Benes 2018-05-29

Log in to post a comment.