compose Multiply/Screen and clip 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
pjj
Posts: 2
Joined: 2014-11-12T15:26:15-07:00
Authentication code: 6789

compose Multiply/Screen and clip mask

Post by pjj »

Hi,
I've been struggling with this for a while and somehow IM doesn't act as I expect. Hopefully you can guide me to the right direction.

I have an image, which has transparency and I would need to apply a Multiply or Screen compose, but so that the transparent areas don't get affected. However, despite dozens of tries with different approaches I still cannot get my head around this. Here are some test files I use in the following examples: https://www.dropbox.com/sh/d5hlb0mzuvjo ... 5Fbla?dl=0, there are also example files (photoshop-screen.tif, photoshop-multiply.tif) of how the desired effect looks like when created in Photoshop.

Code: Select all

convert person.tif red-trans.tif -compose Multiply -composite -flatten out_multiply.jpg
--> works fine

Code: Select all

convert person.tif red-trans.tif -compose Screen -composite -flatten out_screen.jpg
--> result is an all-white image; shouldn't Screen function exactly same way as Multiply..?

Code: Select all

convert person.tif -mask person.tif -alpha Extract red-trans.tif -compose Multiply -composite out_multiply2.tif
--> Multiply for red area gets applied correctly, but the transparent area of red-trans.tif doesn't leave the person.tif untouched. Also the transparent areas of person.tif end up black

Code: Select all

convert person.tif -mask person.tif -alpha Extract red-trans.tif -compose Screen -composite out_screen2.tif
--> similar result than from Multiply above

How could I get closest to the result from Photoshop? What is the best way to use image's transparency as a clip mask to the image itself or is there a different way to approach this problem? I've read through the usage examples, but somehow my IM doesn't seem to act the same way. I'm using IM 6.8.9-7 on OS X 10.9.5.

I appreciate any help or suggestions for further tries.
--patrik
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: compose Multiply/Screen and clip mask

Post by fmw42 »

IM 6 has an ideosyncracy that for non-mathematical compose methods done before -flatten, you need to reset the compose method to over before the flatten. I generally just do it before any -flatten, if I have done another compose earlier in the processing command.

This works for me:

convert person.tif red-trans.tif -compose Screen -composite -compose over -flatten out_screen.jpg

or

convert person.tif red-white.tif -compose screen -composite -alpha remove out_multiply5.jpg

But this does not

convert -respect-parentheses \( person.tif red-white.tif -compose screen -composite \) -flatten out_multiply4.png
pjj
Posts: 2
Joined: 2014-11-12T15:26:15-07:00
Authentication code: 6789

Re: compose Multiply/Screen and clip mask

Post by pjj »

Thanks, the Screen works with those first ones! What about using the person.tif's transparency so that the red screen is applied only to non-transparent parts? Ultimately I'd want to achieve an effect, where I apply two images to different parts of the person.tif so that one of the overlays is in Multiply and other one in Screen and both are clipped to the non-transparent areas of person.tif. I added to the dropbox folder above (https://www.dropbox.com/sh/d5hlb0mzuvjo ... 5Fbla?dl=0) a file photoshop-multiply+screen.tif which shows this effect. Can that be achieved?

Is the -respect-parentheses somehow required if parentheses are used? I had never encountered that before...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: compose Multiply/Screen and clip mask

Post by fmw42 »

The respect-parentheses is not always needed. But when things do not work as expected, try adding it to see if that helps. In this case, the compose setting is not restricted to the parens, though it should be.

With respect to doing two operations and keeping the original transparency. You may need to extract the alpha channel, process the image both ways with alpha off or removed, combine the images as needed and then add the alpha channel back using -compose copy_opacity -composite at the end. Since I am not sure how you are processing the two halves and combining, I cannot show you how to do that.

This would have worked on your one operation example.


convert person.tif red-white.tif \
\( -clone 0 -alpha extract \) \
\( -clone 0 -clone 1 -compose Multiply -composite \) \
-delete 0,1 +swap -alpha off -compose copy_opacity -composite out_multiply.png


See
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/basics/#clone
http://www.imagemagick.org/Usage/masking/#alpha
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: compose Multiply/Screen and clip mask

Post by snibgo »

"-respect-parentheses" saves some settings before each open-parenthesis, restoring them afterwards. I don't know if it works properly on nested parentheses, like a stack push and pop.

It only works on some settings, not all. Personally, I never use it because I can never remember what it works on.

A trick to remember: most operations use many of the settings. If a command doesn't do what I think it should, the first question are: What setting have I used? Are any of the settings used by any of the later operations? This often happens when I combine two commands, where the first command created a temporary file and the second took its input from that temporary file.

Buried in my stuff is a list of what operations use what settings. I intend to write this up.
snibgo's IM pages: im.snibgo.com
Post Reply