Antialiased white border around PNG with transparent B/G

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
marcusj

Antialiased white border around PNG with transparent B/G

Post by marcusj »

I've been trying to create an anti-aliased (white) border of 1 pixel around a PNG image that has a transparent background without success.

I used PerlMagick to make a script that I have subsequently found much better done here http://www.fmwconcepts.com/imagemagick/ ... /index.php. Basically, I understand how to make this:

Image

but I don't understand how to add the edge/outline to the original image below in an anti-aliased style.

Image

Is it possible?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Antialiased white border around PNG with transparent B/G

Post by fmw42 »

You can try one of 3 things:

1) use -blur to blur the edge slightly with the transparent background

2) magnify your original image by 2 or 4 in each direction, then extract the edge result, then minimize the result back to your original size (use -filter lanczos -resize)

3) modify the magicwand script to antialias the binary image before it extracts the outline. See the concept behind the feather script at http://www.fmwconcepts.com/imagemagick/ ... /index.php

Hope one of these approaches helps
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Antialiased white border around PNG with transparent B/G

Post by anthony »

Of course creating anti-aliased areas that can piece together (using blend or add composition)
would be the ultimate form of anti-aliased segmentation of an image. However that seems to be much more difficult then a pure B/W mask.

Please let us know what you try and what you come up with, as I and probably a lot of other people would like to know what you conclude from the various methods, and the best result you achieved.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
marcusj

Re: Antialiased white border around PNG with transparent B/G

Post by marcusj »

Great, I've made some progress... ...and sorry for my hopeless lack of understanding of bitmap manipulation.

In my (PerlMagick) script I now make a new image with a transparent background of the same dimensions and then blur it. Then I Composite it onto the original image. Right now I'm using the Screen composite method which seems to get closest to the result.

Actually, I've been looking around the examples pages some more and it looks like the stuff here is really close to the result I want to achieve anyway. That is to say, I want to make an image that is (already) on a transparent background to get a 'nice edge' so that it can be placed on a 'noisy background' and still stand out. The specific example on the examples page starts out with an image on a white background (i.e. no transparent layer). So would I be close with something like the following sequence?
  • convert myImageWithTransparentBackground.png <some command to extract the alpha mask> difference.png
  • convert difference.png -bordercolor black -border 5 -threshold 10% -blur 0x3 halo_mask.png
  • convert myImageWithTransparentBackground.png -bordercolor white -border 5 halo_mask.png +matte -compose CopyOpacity -composite myImageWithTransparentBackgroundAndHalo.png
marcusj

Re: Antialiased white border around PNG with transparent B/G

Post by marcusj »

Finally I have just about settled on a very slightly tweaked version of fmw42's suggestion:
  • Expand the canvas with transparent background to add a 1-pixel border all around, just to make sure that there's going to be room for the outline.
  • Double the size of the image with -resize.
  • Use Fred's magicwand to make an outline (0,0 -t 10 -f mask -m edge -c color="trans" -r inside).
  • Convert the black background (couldn't persuade magicwand to do this) to transparent using convert -channel A -fill none -transparent black.
  • Composite the white outline onto the enlarged image with -compose Over.
  • Halve the size of the composite with -filter Lanczos to get the final image.
Of the ~2000 images, this has worked well for about 95%. That saves me a whole bunch of Gimp / Photoshop time :D .

Thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Antialiased white border around PNG with transparent B/G

Post by fmw42 »

Yes, you are right. My magicwand script only makes a binary outline image and does not allow the black area to be output as transparent. But your modification afterwards is what I would have done. Glad to hear it has worked out.
Post Reply