Remove black edge artifacts from a transparent image

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

Image example

I am looking for a way to automatically clear borders. I tried just to mask the pixels with a specific RGB value, but the images themselves may also contain black pixels, and then they are filtered out. I am using Python3 and opencv3 / PIL.

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

EDIT: As indicated in note usr2564301 below, very few (if any) edge pixels are black. I still need to remove them, so I want to use some threshold value and remove pixels that are neighbors, transparent pixels, and either:

  • The darker the threshold, or
  • Darker at least the threshold than any adjacent opaque pixel.
+5
source share
2 answers

You can smooth the edges of the alpha channel in ImageMagick as follows:

Input:

enter image description here

convert image.png -channel a -blur 0x2 -level 50x100% +channel result.png 

enter image description here

Adjust 2 using lower values ​​than 2 if there is a thinner black frame and more than 2 if there are wider black borders.

+2
source

Try to take the alpha channel and blur it by a couple of pixels. I illustrate the technique with ImageMagick because it is simpler, but you can do the same with OpenCV :

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

enter image description here

+1
source

Source: https://habr.com/ru/post/1275072/


All Articles