Fading image edges

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
4mpm3
Posts: 3
Joined: 2018-03-01T09:12:48-07:00
Authentication code: 1152

Fading image edges

Post by 4mpm3 » 2018-03-01T09:30:13-07:00

This should be really easy. All I need to do is fade the outside edges of a bitmap (no edge or shape detection required). I'd like to go from solid to fully transparent within a 20 or 30 pixel region along the outer edge. Like this Image

I've tried a couple of different approaches, but neither has worked. I tried this:

convert %%f -alpha set -virtual-pixel transparent -channel A -morphology Distance Manhattan ..\processed\%%f

But no matter how I rejig the parameters, I always end up fading the whole image.

I tried this:

convert %%f -alpha set -virtual-pixel transparent -channel A -blur 30x30 -level 50%,100% +channel ..\processed\%%f

But I have the opposite problem--only a tiny bit of fading that doesn't go all the way to fully transparent, and only on the top edge.

I have ImageMagick 6.8.8.2 right now.

snibgo
Posts: 10926
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fading image edges

Post by snibgo » 2018-03-01T10:04:04-07:00

For making transparency, I first create a black/gray/white image. This will be black where I want transparency, white for opaque, and gray in between. I can easily visualise black/gray/white, and I know how to make it darker or lighter.

So, we make a black/gray/white image. There are many ways, for example:

Code: Select all

convert toes.png -fill white -colorize 100 -virtual-pixel Black -blur 0x10 -level 50,100% bgw_edge.png
Image

Then we use the black/gray/white as opacity in the input image:

Code: Select all

convert toes.png bgw_edge.png -alpha off -compose CopyOpacity -compose toes_bgw.png
Image
The two converts can be combined into one, of course.
snibgo's IM pages: im.snibgo.com

User avatar
GeeMack
Posts: 650
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Fading image edges

Post by GeeMack » 2018-03-01T10:51:51-07:00

4mpm3 wrote:
2018-03-01T09:30:13-07:00
This should be really easy. All I need to do is fade the outside edges of a bitmap (no edge or shape detection required). I'd like to go from solid to fully transparent within a 20 or 30 pixel region along the outer edge.
As snibgo mentioned, there are many ways to achieve this result. Here's a variation that can run from a bash command line and should work on almost any IM version at least as far back as 6.7.7-10...

Code: Select all

convert input.png -bordercolor black -fill white \
   \( -clone 0 -colorize 100 -shave 10x10 -border 10x10 -blur 0x10 \) \
   -compose copyopacity -composite output.png
That builds the mask inside the parentheses, then uses the compose method "copyopacity" to create the transparent edges.

The way IM handles alpha has been modified over the years, so the fade amount may be slightly different from version to version.

4mpm3
Posts: 3
Joined: 2018-03-01T09:12:48-07:00
Authentication code: 1152

Re: Fading image edges

Post by 4mpm3 » 2018-03-01T11:28:09-07:00

GeeMack wrote:
2018-03-01T10:51:51-07:00
As snibgo mentioned, there are many ways to achieve this result. Here's a variation that can run from a bash command line and should work on almost any IM version at least as far back as 6.7.7-10...

Code: Select all

convert input.png -bordercolor black -fill white \
   \( -clone 0 -colorize 100 -shave 10x10 -border 10x10 -blur 0x10 \) \
   -compose copyopacity -composite output.png
That works, thanks so much! Am I correct in thinking the 10 in the -shave and -border parameters is setting the border region 10 pixels thick, and so I can up this if I want a thicker fade? Also, what does the first 0 in the blur 0x10 parameter signify?

Thanks again!

User avatar
GeeMack
Posts: 650
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Fading image edges

Post by GeeMack » 2018-03-01T11:57:27-07:00

4mpm3 wrote:
2018-03-01T11:28:09-07:00
That works, thanks so much! Am I correct in thinking the 10 in the -shave and -border parameters is setting the border region 10 pixels thick, and so I can up this if I want a thicker fade? Also, what does the first 0 in the blur 0x10 parameter signify?
A copy of the input image is created Inside the parentheses, then it's colored entirely white. That "-shave" removes 10 pixels from all the edges, then the "-border" adds 10 pixels of black to restore the size of the original canvas before the "-blur".

To get a particular amount of fade you can adjust the thickness of the shave and border as well as adjusting the amount of blur. Generally you'll want the shave/border to be as thick as your blur so you have almost fully transparent pixels at the very edges.

The arguments for the "-blur" operator are described at THIS link.

snibgo
Posts: 10926
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Fading image edges

Post by snibgo » 2018-03-01T12:14:11-07:00

4mpm3 wrote:Am I correct in thinking ...
You can add "+write" just before the close-parenthesis, and then view that image, to see how it works:

Code: Select all

convert input.png -bordercolor black -fill white \
 \( -clone 0 -colorize 100 -shave 10x10 -border 10x10 -blur 0x10 \
+write abc.png \) \
 -compose copyopacity -composite output.png
Then you can play with the options, such as the blur, to see what happens.
snibgo's IM pages: im.snibgo.com

4mpm3
Posts: 3
Joined: 2018-03-01T09:12:48-07:00
Authentication code: 1152

Re: Fading image edges

Post by 4mpm3 » 2018-03-01T18:13:13-07:00

Thanks again. ImageMagick is a lifesaver for batch processing large numbers of pictures.

Post Reply