Adrian
Adrian

Reputation: 16725

Batch cleanup of raggedy edges on PNG files

I have a PNG image (actually, a whole bunch of 'em) that was born with a background and was replaced with a transparency. I'm not sure how it got that way, but there are a bunch of raggedy edges in the file I received that I need to remove.

I know how to resolve this issue in GIMP/Photoshop, but I have a stack of these things to fix--I need to use ImageMagick (or some other command line utility). My desired result is clean edges on the image while retaining the alpha channel transparency and color of the originals on them.

I have used ImageMagick in the past to convert file formats and resize images, but I don't have much experience with it beyond that.

I've tried a lot of different things, but only one (below) has come close to what I'm trying to achieve.

Here's what I started with...

enter image description here

Here's the best I've been able to accomplish

I used ImageMagick to clean it up with feathering. I used this command:

convert test.png -alpha set -virtual-pixel transparent -channel A -blur 0x1.7 -level 50,75% +channel testFeathered.png

enter image description here

Are there any other methods or techniques anyone would recommend for accomplishing the goal of a smooth edge to the image without mucking up the color?

Upvotes: 1

Views: 957

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207670

Try using potrace to generate a smoothed vector of the opacity and re-applying it back to the original image:

So, first extract the alpha channel to a PGM file:

convert dress.png -alpha extract opacity.pgm

Then smooth the alpha channel with potrace and save as opacitysmooth.pgm:

potrace -b pgm -o opacitysmooth.pgm opacity.pgm

Then replace the opacity of the original image with the smoothed one:

convert dress.png opacitysmooth.pgm -compose copyopacity -composite result.png

enter image description here

Once you get all that understood, you can do it all in one go like this:

convert dress.png -alpha extract pgm:- | 
  potrace -b pgm -o - - | 
  convert dress.png - -compose copyopacity -composite result.png

Upvotes: 1

Related Questions