Page 1 of 1
Multiple opacities in an image
Posted: 2010-04-16T12:14:33-07:00
by snake211
Hi all,
I have a black-and-white image where want to add transparency.
I need multiple transparency levels according to the grey level of each pixel. If a pixel is close to black, it must be opaque (less transparent). If grey, medium transparent, and if white, almost 100% transparent.
Is there a way to do this?
Does a .png format even support such multiple transparencies?
Thanks for your help in advance.
J.R.
Re: Multiple opacities in an image
Posted: 2010-04-16T12:54:35-07:00
by fmw42
try
convert bwimage \( -clone 0 -negate \) \
-alpha off -compose copy_opacity -composite image_alpha.png
Re: Multiple opacities in an image
Posted: 2010-04-16T14:18:48-07:00
by snake211
Wow! In a single line!!
Kudos to you, fmw42! You just saved me!! (bow, bow, bow...)
As I am not very familiar with ImageMagick, I will study how your code works. If it does not take much of your time and effort, a brief explanation would be greatly appreciated (especially, "-alpha off"?? not "on"??)
Thanks a million!
J.R.
Re: Multiple opacities in an image
Posted: 2010-04-16T14:22:41-07:00
by fmw42
snake211 wrote:Wow! In a single line!!
Kudos to you, fmw42! You just saved me!! (bow, bow, bow...)
As I am not very familiar with ImageMagick, I will study how your code works. If it does not take much of your time and effort, a brief explanation would be greatly appreciated (especially, "-alpha off"?? not "on"??)
Thanks a million!
J.R.
The -clone copies the input image. The -negate inverts the grayshades since you wanted black to be opaque and white transparent. The -alpha off turns off any existing alpha channels so that they don't mess up the placing of the second image into the alpha channel of the first image. the -compose copy_opacity -composite is what places the second image (after negating) into the alpha channel of the first image (after taking out any existing alpha channel via the -alpha off.
see
http://www.imagemagick.org/Usage/compose/#copyopacity and
http://www.imagemagick.org/Usage/basics/#image_seq
Re: Multiple opacities in an image
Posted: 2010-05-20T18:27:13-07:00
by anthony
Slightly simplier method using
-alpha and channels.
Code: Select all
convert bwimage -negate -alpha Copy -negate image_alpha.png
This negates the color channels (RGB is the current channel setting)
copys the greyscale to alpha, then negates the colors (but not alpha) back to normal.
Here is a lightly different version of the same thing, but only negates the alpha channel
Code: Select all
convert bwimage -alpha Copy -channel A -negate image_alpha.png
-alpha copy is not used very often,
-alpha shape is much more commonly used so the color channels are reset to 'background' color. That is very useful for making 'fonts' transparent. Though sometime I wish
-alpha shape used the
-fill color instead of the
-background color. You really you are reseting the foreground color, and not the background color!
Sorry I am ranting
There are lots of ways to skin a cat, and what method you use depends on what you want that skin for, and how messy you like the results!
The 'mess' in this case is how the various settings (such as channel) are left after your image processing is complete.
Or how things are handled. For example what if you input image already contains some alpha, and you want to merge the new alpha with the old alpha! All the above techniques will fail in that regard!