Reputation: 586
I have a set of transparent PNG images with black artifacts around the edges, like this:
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
Reputation: 53089
You can antialias the edges of the alpha channel in ImageMagick as follows:
Input:
convert image.png -channel a -blur 0x2 -level 50x100% +channel result.png
Adjust the 2 using smaller values than 2 if thinner black border and larger than 2 if broader black borders.
Upvotes: 2
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
Upvotes: 2