Why do "-compose" settings inside parens affect operations outside of the parens?

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
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Why do "-compose" settings inside parens affect operations outside of the parens?

Post by sas »

Take, for example, a simple composite operation (blending a rose with a red background):

Code: Select all

convert rose: \( -size 70x46 xc:red \) \( -compose Blend -define compose:args=50% -composite \) red.png
Image

So far so good. But now add a seemingly innocuous "-flatten" at the end, and the result changes unexpectedly:

Code: Select all

convert rose: \( -size 70x46 xc:red \) \( -compose Blend -define compose:args=50% -composite \) -flatten flat.png
Image

Apparently, the "-flatten" operation is affected by the "-compose Blend" setting, even though that setting was placed inside a pair of parenthesis. Explicitly resetting "-compose" to its default value "Over" before flattening, fixes it:

Code: Select all

convert rose: \( -size 70x46 xc:red \) \( -compose Blend -define compose:args=50% -composite \) -compose Over -flatten over.png
Image

Why is this happening?
I thought parenthesis can be used to limit the scope of settings/flags to only those operations inside the same parenthesis?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Why do "-compose" settings inside parens affect operations outside of the parens?

Post by fmw42 »

You can try using -respect-parenthesis, but in my experience this has been this way in IM 6. Call it a feature or bug. Some settings are not reset automatically nor even do they honor -respect-parenthesis. Compose is one of these that needs to be reset. So I always reset it to -compose over before -flatten or after using mathematical compose settings before changing to some other such as copy_opacity.
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: Why do "-compose" settings inside parens affect operations outside of the parens?

Post by sas »

Good to know, thanks!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Why do "-compose" settings inside parens affect operations outside of the parens?

Post by snibgo »

sas wrote:... even though that setting was placed inside a pair of parenthesis.
Don't forget: the main purpose of open-parenthesis is to push the list of images so far on to a stack, and start a new (empty) list. Close-parenthesis pops the stack.

After an open-parenthesis, your list of images is empty. So you shouldn't have an operator like "-composite" because there are no images in the current list. IM v6 is lax at checking these syntax errors. v7 is more strict, and your first command fails with v7 magick.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Why do "-compose" settings inside parens affect operations outside of the parens?

Post by fmw42 »

Code: Select all

convert rose: \( -size 70x46 xc:red \) \( -compose Blend -define compose:args=50% -composite \) -flatten flat.png
Snibgo is correct. I had not noticed that before. Your second parenthesis has no images to work from. You need at least two images or clones there before you can do a composite in parenthesis. So you probably want

Code: Select all

convert rose: \( -size 70x46 xc:red \) \( -clone 0,1 -compose Blend -define compose:args=50% -composite \) -compose over -flatten flat.png
or

Code: Select all

convert rose: \( -clone 0 -fill red -colorize 100  \) \( -clone 0,1 -compose Blend -define compose:args=50% -composite \) -compose over -flatten flat.png
or perhaps you just want

Code: Select all

convert rose: \( -size 70x46 xc:red \) -compose Blend -define compose:args=50% -composite flat.png

If you add clones, in the earlier commands, then you may need to delete some of the originals.

Sorry I am not sure what your end goal is, functionally.
sas
Posts: 44
Joined: 2009-11-28T16:35:46-07:00
Authentication code: 8675309

Re: Why do "-compose" settings inside parens affect operations outside of the parens?

Post by sas »

snibgo wrote:After an open-parenthesis, your list of images is empty. So you shouldn't have an operator like "-composite" because there are no images in the current list.
Somehow it still worked though - the images I uploaded in the top post, are the result I got and it clearly blended the rose with the red canvas... :)

I get you point though. I'll be careful not to add any incorrect/redundant parens from now on, in order to ensure compatibility with IM7.
fmw42 wrote:Sorry I am not sure what your end goal is, functionally.
This is part of a larger problem where I'm doing lots of steps in a single 'convert' command. It all works fine, now that I manually reset the '-compose' setting before calling '-flatten' later on.

The example commands I showed in the top post, weren't really meant to be useful by themselves, they're just what I got when I removed all unrelated parts in order to demonstrate the issue with parens not limiting setting scope.
Everything's cleared up now though. Thanks for explaining it...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Why do "-compose" settings inside parens affect operations outside of the parens?

Post by snibgo »

sas wrote:Somehow it still worked though ...
Yes. IM v6 kind of starts a new (empty) list when the "(" is encountered, but not really until the first image is encountered. So if there are no images within the parentheses, the previous list is used. This can result in weird processing that is hard to predict.
snibgo's IM pages: im.snibgo.com
Post Reply