Nee
Nee

Reputation: 586

Removing black edge artifacts from a transparent image

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:

Upvotes: 6

Views: 2024

Answers (2)

fmw42
fmw42

Reputation: 53089

You can antialias 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 the 2 using smaller values than 2 if thinner black border and larger than 2 if broader black borders.

Upvotes: 2

Mark Setchell
Mark Setchell

Reputation: 207405

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

Upvotes: 2

Related Questions