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
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.
Fading image edges
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Fading image edges
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:
Then we use the black/gray/white as opacity in the input image:
The two converts can be combined into one, of course.
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
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
The two converts can be combined into one, of course.
snibgo's IM pages: im.snibgo.com
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Fading image edges
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
The way IM handles alpha has been modified over the years, so the fade amount may be slightly different from version to version.
Re: Fading image edges
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?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
Thanks again!
- GeeMack
- Posts: 718
- Joined: 2015-12-01T22:09:46-07:00
- Authentication code: 1151
- Location: Central Illinois, USA
Re: Fading image edges
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.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Fading image edges
You can add "+write" just before the close-parenthesis, and then view that image, to see how it works:4mpm3 wrote:Am I correct in thinking ...
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
snibgo's IM pages: im.snibgo.com
Re: Fading image edges
Thanks again. ImageMagick is a lifesaver for batch processing large numbers of pictures.