I have a set of transparent PNG images with black artifacts around the edges, like this:

[![Example image](https://i.stack.imgur.com/Hwcom.png)](https://i.stack.imgur.com/Hwcom.png)

I'm looking for a way to clean up the borders automatically. I tried simply masking out pixels under a certain RGB value, but the images themselves can also contain black pixels, and those then get filtered out. I'm using Python3 and opencv3/PIL.

My question is: **How can I get rid of the black edges, while preserving black pixels that are not part of an edge?**

EDIT: As usr2564301 pointed out below, very few (if any) of the edge pixels are pure black. I still need to remove them, so I'd want to use some threshold value and **remove pixels that are neighbors to a transparent pixel and are either:**

* **Darker than the *threshold*,** or
* **Darker by at least *threshold* than any neighboring non-transparent pixel.**