How to remove grey/black artifacts at the edges of a png with transparent background image using ImageMagick

44 Views Asked by At

I've noticed that there are a few black or gray pixels along the edges of my png image with transparent background, and I'd like to clean them up using ImageMagick.

Could you provide some guidance on how to remove these black or gray edge pixels while ensuring that other black or gray pixels within the image are not affected?

Your assistance would be greatly appreciated. Thank you!

enter image description here

1

There are 1 best solutions below

7
Mark Setchell On

Mmm, firstly I don't see any black or grey pixels in your image at the edges or elsewhere?

If I flatten your image onto a lime background, I should be able to see them:

magick glow.png -background lime -flatten result.png

enter image description here

Let's look at your image's constituent channels by separating them out and laying them out R, G, B, A across the page with a 10 px gap between channels:

magick glow.png -separate +smush 10 channels.png 

enter image description here

Let's assume your question really wants to slightly increase the size of the transparent area (i.e. the black surround in the right-most channel) so that we don't see the jagged edges of the other (RGB) channels. We can do that by eroding the white area slightly:

magick glow.png -resize 400x -channel A -morphology erode disk:20 result.png

enter image description here

If we separate out the channels again, you can see what has happened in the alpha channel on the right:

magick result.png -separate +smush 10 channels.png 

enter image description here

I have exaggerated the effect by reducing the size of your enormous image and using a large disk. You should experiment with the disk size till you get the result you need.