Page 1 of 1
Why do "-compose" settings inside parens affect operations outside of the parens?
Posted: 2016-04-13T12:40:08-07:00
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
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
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
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?
Re: Why do "-compose" settings inside parens affect operations outside of the parens?
Posted: 2016-04-13T12:51:43-07:00
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.
Re: Why do "-compose" settings inside parens affect operations outside of the parens?
Posted: 2016-04-13T13:03:04-07:00
by sas
Good to know, thanks!
Re: Why do "-compose" settings inside parens affect operations outside of the parens?
Posted: 2016-04-13T13:18:50-07:00
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.
Re: Why do "-compose" settings inside parens affect operations outside of the parens?
Posted: 2016-04-13T14:31:24-07:00
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.
Re: Why do "-compose" settings inside parens affect operations outside of the parens?
Posted: 2016-04-14T01:59:58-07:00
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...
Re: Why do "-compose" settings inside parens affect operations outside of the parens?
Posted: 2016-04-14T05:19:16-07:00
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.