What you need is called a morphological skeletonize operation or medial axis transform. It's a common operation in image processing. There are many libraries that can perform this task, but some sort of context in which you perform the operation would be useful.