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.
Multiple opacities in an image
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Multiple opacities in an image
try
convert bwimage \( -clone 0 -negate \) \
-alpha off -compose copy_opacity -composite image_alpha.png
convert bwimage \( -clone 0 -negate \) \
-alpha off -compose copy_opacity -composite image_alpha.png
Re: Multiple opacities in an image
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.
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.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Multiple opacities in an image
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.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.
see http://www.imagemagick.org/Usage/compose/#copyopacity and http://www.imagemagick.org/Usage/basics/#image_seq
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Multiple opacities in an image
Slightly simplier method using -alpha and channels.
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
-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!
Code: Select all
convert bwimage -negate -alpha Copy -negate image_alpha.png
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
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!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/