Hi everyone,
after some 6 hours of fiddling around and searching the WebI I am staring to get a little desperate... Somehow I just seem to be incapable of understanding home IM handles transparency (as opposed to, say, GIMP). Hence I finally decided to just ask for help on the following task:
I hava a (png) image containing semi-transparent pixels. Furthermore, I am generating on-the-fly a grayscale image (using a draw and a blur operation). Now I would like to use the grayscale image as a mask for modifying the png image's transparency so that image pixels conciding with white mask pixels retain their current transparency value while those being masked with black pixels become fully transparent. In other words, I would like to multiply the png image's opacity values with the mask's intensity values.
Any hints how to achieve the desired effect? (I am using PerlMagick, but since I am interested in the general procedure I am posting here rather than in the PerlMagick forum).
Thanks in advance for any help --
tcrass
Increasing transparency using a grayscale mask
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Increasing transparency using a grayscale mask
I cannot tell you how to do it in Perlmagick, but in command line, you would extract the alpha channel of the image, multiply by the mask and then remove the old alpha channel and put the new result back into the alpha channel.
convert image.png \( -clone 0 -alpha extract maskimage -compose multiply -composite \) \
-alpha off -compose copy_opacity -composite result.png
see http://www.imagemagick.org/Usage/compose/
convert image.png \( -clone 0 -alpha extract maskimage -compose multiply -composite \) \
-alpha off -compose copy_opacity -composite result.png
see http://www.imagemagick.org/Usage/compose/
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Increasing transparency using a grayscale mask
The better way is convert your gray-scale make into a 'shaped mask' using -alpha copy (you do not need to color it using -alpha shape).
You can then use the Duff-Porter Alpha Composition method. such as 'Dst-In'
http://www.imagemagick.org/Usage/compose/#dstin
That will find the parts of the original (destination) image that are 'INSIDE' the shaped mask.
Colors all come form the destination image, only the shape of the mask is used.
Similarly you can use Dst_Out to get the parts NOT-IN the shape mask area.
http://www.imagemagick.org/Usage/compose/#dstout
The two parts can be merged back together to re-produce the exact original image using 'Plus'
This aspect is currently 'in development' in IM examples in Channels and Masking
Aligning Masked Images...
http://www.imagemagick.org/Usage/channels/#aligning
This is a LOT simpler than mucking around with grey-scale masks and mathematics, and image juggling, and possibility getting things wrong.
You can then use the Duff-Porter Alpha Composition method. such as 'Dst-In'
http://www.imagemagick.org/Usage/compose/#dstin
That will find the parts of the original (destination) image that are 'INSIDE' the shaped mask.
Colors all come form the destination image, only the shape of the mask is used.
Similarly you can use Dst_Out to get the parts NOT-IN the shape mask area.
http://www.imagemagick.org/Usage/compose/#dstout
The two parts can be merged back together to re-produce the exact original image using 'Plus'
This aspect is currently 'in development' in IM examples in Channels and Masking
Aligning Masked Images...
http://www.imagemagick.org/Usage/channels/#aligning
This is a LOT simpler than mucking around with grey-scale masks and mathematics, and image juggling, and possibility getting things wrong.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Increasing transparency using a grayscale mask
Hi fmw42,

Anthony,
Apart from that: Thanks a lot for maintaining those wonderful ImageMagic 'usage' pages, which I frequently enjoy just browsing through!
Best regards --
tcrass
thanks a lot, '-alpha extract' was what I needed, and it was very easey to implement the above procedure in PerlMagick.fmw42 wrote: convert image.png \( -clone 0 -alpha extract maskimage -compose multiply -composite \) \
-alpha off -compose copy_opacity -composite result.png

Anthony,
I only partially agree with that -- I usually find it easyer to apply some clean maths than to dig myself through a plethora of not-so-concisely defined terminology. (Well, at least that's my impression with ImageMagick's alpha/matte/transparency/mask stuff.))anthony wrote:The better way is convert your gray-scale make into a 'shaped mask' using -alpha copy (you do not need to color it using -alpha shape).
[...]
This is a LOT simpler than mucking around with grey-scale masks and mathematics, and image juggling, and possibility getting things wrong.
Apart from that: Thanks a lot for maintaining those wonderful ImageMagic 'usage' pages, which I frequently enjoy just browsing through!
Best regards --
tcrass