<p>I'm trying to convert images that have a single object with an outline to a transparent background.  I'm currently using the image/png package in golang but can switch to any language thats more appropriate for the task.</p>
<p>What i'm trying so far is to floodfill the white surrounding background with transparency, convert the outline to a transparency and then do the floodfill again to catch the white area surrounding the image.  This isn't working well so far.  I've tried using a two phase approach where I do a floodfill with a distance tolerance to get the white area outside of the outline. A similar approach to removing the outline then another tolerance floodfill to get the rest of the white area inside the outline.</p>
<p>My approaches have been too simple when it comes to the outline though and while I get parts of it, I can't come up with an approach that gets the whole outline taking into account the various shading. One approach, came closest to working which is to &quot;erase&quot; the area within a fixed number pixels away from the outer white area but even that had problems.</p>
<p>Here are examples of before and after images that demonstrate what i'm trying to accomplish.  This seems like it should be simple but i'm stuck.</p>
<p>Thanks in advance for any help.</p>
<p><a href="https://i.sstatic.net/fo1aT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fo1aT.png" alt="image without transparency" /></a></p>
<p><a href="https://i.sstatic.net/FoYwb.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/FoYwb.jpg" alt="image with transparency (hand processed)" /></a></p>

<transparency>
Automatically convert field of white and an outline to transparent background
tcp3
# Automatically convert field of white and an outline to transparent background

(Asked by: username_0 on 2023-08-05)

I'm trying to convert images that have a single object with an outline to a transparent background. I'm currently using the image/png package in golang but can switch to any language thats more appropriate for the task.

What i'm trying so far is to floodfill the white surrounding background with transparency, convert the outline to a transparency and then do the floodfill again to catch the white area surrounding the image. This isn't working well so far. I've tried using a two phase approach where I do a floodfill with a distance tolerance to get the white area outside of the outline. A similar approach to removing the outline then another tolerance floodfill to get the rest of the white area inside the outline.

My approaches have been too simple when it comes to the outline though and while I get parts of it, I can't come up with an approach that gets the whole outline taking into account the various shading. One approach, came closest to working which is to "erase" the area within a fixed number pixels away from the outer white area but even that had problems.

Here are examples of before and after images that demonstrate what i'm trying to accomplish. This seems like it should be simple but i'm stuck.

Thanks in advance for any help.

[![image without transparency](https://i.sstatic.net/fo1aT.png)](https://i.sstatic.net/fo1aT.png)

[![image with transparency (hand processed)](https://i.sstatic.net/FoYwb.jpg)](https://i.sstatic.net/FoYwb.jpg)

**Tags:** transparency

---

## 0 answers

