Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing black edge artifacts from a transparent image

Tags:

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

Example image

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.
like image 558
Nee Avatar asked Jan 31 '18 10:01

Nee


1 Answers

Try taking the alpha channel and eroding it by a couple of pixels. I am illustrating the technique with ImageMagick because that's easier, but you can do the same thing with OpenCV:

convert pinkboythingwithcathead.png \( +clone -alpha extract -morphology erode disk:2 \) -compose copy-alpha -composite result.png

enter image description here

like image 65
Mark Setchell Avatar answered Sep 20 '22 12:09

Mark Setchell