I’ve made a sketch which does just this a while back. It was originally a canny attempt so essentially its calculating the skeleton of the edges, but is also able to calculate contours.
Theres currently a severe lack of comments on it and some of the other functions are nothing more than work in progress, but I’m happy to add comments and answer any questions you may have.
As for the algorithm used I just create a sobel of an image then for each black point I travel up down left right and diagonal in every direction until I find a white pixel store the steps required and then place a black pixel where the step count in opposite directions are equal to each other.