Increasing transparency using a grayscale mask

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
tcrass
Posts: 38
Joined: 2009-11-22T11:12:52-07:00
Authentication code: 8675309

Increasing transparency using a grayscale mask

Post by tcrass »

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
User avatar
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

Post by fmw42 »

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/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Increasing transparency using a grayscale mask

Post by anthony »

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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tcrass
Posts: 38
Joined: 2009-11-22T11:12:52-07:00
Authentication code: 8675309

Re: Increasing transparency using a grayscale mask

Post by tcrass »

Hi fmw42,
fmw42 wrote: convert image.png \( -clone 0 -alpha extract maskimage -compose multiply -composite \) \
-alpha off -compose copy_opacity -composite result.png
thanks a lot, '-alpha extract' was what I needed, and it was very easey to implement the above procedure in PerlMagick. :-)

Anthony,
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.
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.))

Apart from that: Thanks a lot for maintaining those wonderful ImageMagic 'usage' pages, which I frequently enjoy just browsing through!

Best regards --

tcrass
Post Reply