Transparent "feathering"

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
jrweir

Transparent "feathering"

Post by jrweir »

I am trying to remove a color from an image (ie, make transparent), but along the edges of transparency create a feathered effect where it fades from transparent to opaque over the remaining colors.
The idea is to start with an image like this
Image
and make it look like this
Image

I've come to a dead end of ideas, the closest I have gotten is by the following:

Code: Select all

convert source.png -transparent yellow no-yellow.png
convert no-yellow.png -channel matte -separate +matte reverse-matte.png
convert reverse-matte.png -negate matte.png
convert matte.png -blur 0x10 blur-matte.png
convert source.png  blur-matte.png +matte -compose CopyOpacity -composite result.png
which produces:
Image
Obviously the blurred matte Image has half it's gradient inside the original yellow circle so applying the matte will produce the above, which is not what I'm after.

Thanks for any help with this problem.
Jeremy
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Transparent "feathering"

Post by Bonzo »

I do not know if this is what you are trying to acheive but I have used a bit of code from Anthony's examples to blend 2 images together.

An example on this page - www.rubblewebs.co.uk/imagemagick/other.php - one hard edged blend and one soft edge blend.
jrweir

Re: Transparent "feathering"

Post by jrweir »

Thanks, that's not exactly what I was looking for though.

I found another solution to the larger problem which got around doing this at all. I didn't figure the feathering problem out, so if anyone knows I'm still interested, but it isn't necessary for me.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Transparent "feathering"

Post by anthony »

Use the original image to create a black and white mask, which you blured. I see you have that.

But if you want anything that is yellow to be absolutely transparent, then you need to adjust the blured mask. Blur not only leaks black (transparent) into the white, but also the white (opaque) into the black. That is why you can still see yellow color!!!!

That is at the border. you get a 50% grey in the mask. And not pure black 9for fully transparent). In other words, a simple blur is not good for feathering images. You need to adjust your blured mask so anything that is 50% grey or darker becomes fully black.

This is probably the simplist solution.
convert blured_mask.png -negate -evaluate multiply 2 -negate feather_mask.png

WARNING: bluring like this will make a feathered masking image, but it is NOT the best way. If you get two parts of the image close together or only a very small transparent hole, the blur will not getnerate a 50% grey at those edges and the overall color of the area also effects the blur. that is for a complex shape, the result will still not be a feathered mask!

Does anyone know the algorithm to properly feather a mask? And remember this is not a simple 'blur' when the shape is also complex! this algorithm is something that should probably be implemented into ImageMagick.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
nnnicck

Re: Transparent "feathering"

Post by nnnicck »

Your original code is all set except the blur goes into and out of the yellow. To fix this on layer masks in Photoshop I typically push the levels of the mask one way or another. Try this after generating blur-matte.png, I think it should do what you want:

Code: Select all

convert blur-matte.png -level 40000x65000x1 tight-blur-matte.png
convert source.png tight-blur-matte.png  +matte -compose CopyOpacity -composite result.png
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transparent "feathering"

Post by fmw42 »

Note you will need to create a checkerboard pattern such as in my ps_checks.gif which is 8x8 pixel squares alternating white and #cdcdcd (which tries to match that of the checkerboard background from Photoshop's transparency selector).

You can also use the IM internal pattern: checkerboard, but it looks different.

Here is the command for ps_checks.gif:

convert -size 8x8 xc:white xc:"#cdcdcd" +append \
\( +clone -flop \) -append ps_checks.gif



Now, try this if on Linux or Mac:

bgcolor=`convert source.png -format "%[pixel:s.p{0,0}]" info:`
convert source.png \( -clone 0 -tile ps_checks.gif -draw "color 0,0 reset" \) \
\( -clone 0 -fill "$bgcolor" -colorize 100% \) \
\( source.png -channel r -separate +channel -threshold 0 -negate -blur 0x12 -level 40x100% \) \
-delete 0 -compose over -composite source_result.png

Image

You can adjust it with the blur amount and the first value in -level.


This works without having to create a constant green image:

bgcolor=`convert source.png -format "%[pixel:s.p{0,0}]" info:`
convert source.png \( -clone 0 -tile ps_checks.gif -draw "color 0,0 reset" \) \
\( source.png -channel r -separate +channel -threshold 0 -negate -blur 0x12 -level 40x100% -negate \) \
-delete 0 -alpha off -compose copy_opacity -composite -compose over -background "$bgcolor" -flatten source_result.png


This works without having to get the color first, but is a bit more awkward as it has to specify the input image again:

convert source.png \( -clone 0 -tile ps_checks.gif -draw "color 0,0 reset" \) \
\( -clone 0 -tile source.png[1x1+0+0] -draw "color 0,0 reset" \) \
\( source.png -channel r -separate +channel -threshold 0 -negate -blur 0x12 -level 40x100% \) \
-delete 0 -compose over -composite ssource_result.png


Alternately (and simplest), try

convert source.png \
\( -clone 0 -tile ps_checks.gif -draw "color 0,0 reset" \) \
\( -clone 0 -channel r -separate +channel -threshold 0 -negate -blur 0x12 -level 60x100% \) \
-swap 0,1 -compose over -composite source_result2.png

Image

But if you lower the -level 60x100% too much below 60, you will start seeing the yellow.


If on windows, then see http://www.imagemagick.org/Usage/windows
Post Reply