Black border in image

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
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Black border in image

Post by SharkD »

I was wondering if there were any way in ImageMagick of getting the black border around the object in the second image below, starting with the first image. I can do it in The Gimp, but I'd hoped that there was a way of automating the process. Note that the first image is a 32-bit image with an alpha channel. The second one is 24-bit without an alpha channel. Thanks!

Image
Image
Last edited by SharkD on 2008-01-07T19:56:17-07:00, edited 3 times in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Black border in image

Post by anthony »

A little confused. the pointers from those images don't match the smaller image shown. It ist just not clear what your start and end point is.

Does the original have transparency? Or do I have to first replace black with blue? Border around the individual objects or the whole image?

Note all these steps are described in IM Examples, Channels, Transparency, and Masking
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Re: Black border in image

Post by SharkD »

What do you mean, "pointers"?

I edited the first post so that the images shown are no longer thumbnails, but the images themselves. Hopefully that clears up any confusion.

I am aware of how to remove the alpha channel and change the background color (though I included it in case there's a quicker way of doing it by combining commands or otherwise using fewer steps). What I want to know is how to add the black border around the objects. In The GIMP I do this by creating a selection around the objects, expanding the selection by one pixel, inverting the selection, and turning everything within the inverted selection to a blue color. How do I do this in ImageMagick?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Black border in image

Post by anthony »

Okay. To create a non-rectangular border around the object you have a number of methods.
First create a mask of the objects on a transparent background by just using -colorize to make all non-transparent colors black.

Now you need to expand that mask by the thickness of the border you want to add. You can blur it then adjust the transparency of the result to create a nice fuzzy / anti-aliased border. Just how fuzzy and thick it is depends on your blur and semi-transparency adjustments.

One example of this is doubling of the transparency for soft outlines as blur not only blur the insize of the mask out, but also the outside in. http://imagemagick.org/Usage/convolve/#shadow_outline

Other semi-transparency adjustments can make a much stronger and more distinct outline.

Once you have adjusted the blured mask, you underlay it (see the link above) under the objects.


The other method also takes the same mask but specifically expands the edge by one or more pixels. Fred Wienhaus has a script called "morphology" for this in his IM scripts area
http://www.fmwconcepts.com/imagemagick/

However this may only generate a boolean border that has no smoothed anti-aliased edges to it. however if the bordered objects are being saved to GIF that may be exactly what you are after.

Again after the black mask is expanded you underlay it under the original image to add the border, before adding any background color or pattern.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Re: Black border in image

Post by SharkD »

anthony wrote:Now you need to expand that mask by the thickness of the border you want to add. You can blur it then adjust the transparency of the result to create a nice fuzzy / anti-aliased border. Just how fuzzy and thick it is depends on your blur and semi-transparency adjustments.
How do you expand the mask so that the border around the objects is exactly one pixel? It's easy to expand the mask itself by one pixel, but it's not the same. I don't think I can even do it using trigonometry, as each object is a different size and doesn't have a regular shape.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Black border in image

Post by anthony »

SharkD wrote: How do you expand the mask so that the border around the objects is exactly one pixel? It's easy to expand the mask itself by one pixel, but it's not the same. I don't think I can even do it using trigonometry, as each object is a different size and doesn't have a regular shape.
Expand the mask, and convert it into a shape which you can then underlay under the original image.

See IM Examples, Channels and Masks, Masks as Colors Shapes
http://imagemagick.org/Usage/channels/#shapes

Other parts of the same page talk about extarcting the mask. I have not converted Fred Wienhaus's "morphology" script into raw IM examples of mask modifications, as yet, though plan to do so.

Better still I'd like to see some of thos options merged into IM core library operations. The problem is I don't have the time, and Fred is not up of C programming, though his scripts do provide proof of concept of the methods.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
SharkD
Posts: 35
Joined: 2006-07-30T13:18:31-07:00

Re: Black border in image

Post by SharkD »

anthony wrote:
SharkD wrote: How do you expand the mask so that the border around the objects is exactly one pixel? It's easy to expand the mask itself by one pixel, but it's not the same. I don't think I can even do it using trigonometry, as each object is a different size and doesn't have a regular shape.
Expand the mask, and convert it into a shape which you can then underlay under the original image.
That doesn't answer my question.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Black border in image

Post by anthony »

I don't really want to hold your hand and walk you through every step but...

Download an image with existing transparency, and add soem extra working space around it

Code: Select all

  convert http://imagemagick.org/Usage/images/hand_point.gif \
    -bordercolor none -border 4x4 hand.gif
Now make a black shape mask...

Code: Select all

  convert hand.gif -fill black -colorize 100% hand_shape.png
Now lets use -blur to expand that shape.

Code: Select all

  convert hand_shape.png -channel RGBA -blur 0x4 hand_blur.png
This mask is very 'fuzzy', but we can now adjust the alpha channel histogram to set the border we want. Now I am going to assume you are the result to be suitable for GIF, and as such has a purely on/off (boolean) transparency. This means we only have to threshold the alpha channel to get a larger shape...

Code: Select all

   convert hand_blur.png -channel A -threshold 80% hand_exp.png
Now layer the images together again...

Code: Select all

  convert hand_exp.png hand.gif -composite hand_bordered.gif
The blur method may have reduced borders at sharp corners of the original shape. As such you may be better expanding the shape using Fred Wienhaus's scripts, rather than using blur.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply