convert: parenthesis nested too deeply

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
dovlan
Posts: 6
Joined: 2013-07-19T15:52:19-07:00
Authentication code: 6789

convert: parenthesis nested too deeply

Post by dovlan »

Hi,

I am using IM to modify and compose large number of images on top of larger background. This is the basic example which works fine:

convert \( \( -size 1920x1080 xc:white \) \( -resize 250x250! icon_1.png -repage 250x250+0+0 \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) -geometry +1500+500 -composite \) \( -resize 250x250! icon_1.png -repage 250x250+0+0 \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) -geometry +1000+500 -composite output.jpg

Once the number of icons increase ( in my case to 34) i start getting error :

convert: parenthesis nested too deeply `(' @ convert.c/ConvertImageCommand/542.

Real-life example has a lot of dropshadow effects, alpha channel modifications, texts etc and spans across 10-15 lines. I was unable to find limit for parenthesis nesting in documentation.
Is it a familiar limit i have reached or have i made an error somewhere? Any way to circumvent it?

TIA
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert: parenthesis nested too deeply

Post by fmw42 »

I think you may be nesting too deep when you do not need to. It helps to put line breaks at appropriate places to help look at the processing more logically. Also for a series of successive composites, they do not generally need parenthesis. You can use -compose over -composite on pairs of images. Once the pair has been composited, you can simple put in one more image and -compose over -composite command to add the next image. So you only need the parenthesis to keep the processing of each image separate or for a clone.

see
http://www.imagemagick.org/Usage/layers/#convert

I am not sure I totally follow your command. It would appear that you have commands in the wrong order (the image should come before any resize or other operator type command). And the repages are doing nothing as far as I can tell and are not needed following a resize.
convert \( \( -size 1920x1080 xc:white \) \( -resize 250x250! icon_1.png -repage 250x250+0+0 \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) -geometry +1500+500 -composite \) \( -resize 250x250! icon_1.png -repage 250x250+0+0 \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) -geometry +1000+500 -composite output.jpg
Let me know if the following works or if I have misunderstood something in your processing.

convert \( -size 1920x1080 xc:white \) \
\( icon_1.png -resize 250x250! +clone -background black -shadow 30x3+5+5 +swap -background none -layers merge +repage \) \
-geometry +1500+500 -composite \
\( icon_1.png -resize 250x250! +clone -background black -shadow 30x3+5+5 +swap -background none -layers merge +repage \) \
-geometry +1000+500 -composite \
output.jpg
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: convert: parenthesis nested too deeply

Post by glennrp »

The limit appears to be 32:
wand/mogrify-private.h:#define MaxImageStackDepth 32
dovlan
Posts: 6
Joined: 2013-07-19T15:52:19-07:00
Authentication code: 6789

Re: convert: parenthesis nested too deeply

Post by dovlan »

glennrp wrote:The limit appears to be 32:
wand/mogrify-private.h:#define MaxImageStackDepth 32
And i had 34 stacked images in my real-life case. Splitting into two commands should be enough/
Thanks
dovlan
Posts: 6
Joined: 2013-07-19T15:52:19-07:00
Authentication code: 6789

Re: convert: parenthesis nested too deeply

Post by dovlan »

Hi,
i have just tested the suggested order of commands. Here is the command i used ( I changed dimensions ):

convert \( -size 300x300 xc:blue \)
\( icon_1.png -resize 150x150! +clone -background black -shadow 30x3+5+5 +swap -background none -layers merge +repage \) -geometry +0+0 -composite
\( icon_1.png -resize 150x150! +clone -background black -shadow 30x3+5+5 +swap -background none -layers merge +repage \) -geometry +150+150 -composite
output_not_ok.jpg
Result is Image
And here is my order
convert \( \( -size 300x300 xc:blue \)
\( -resize 150x150! icon_1.png -repage 150x150+0+0 \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) -geometry +0+0 -composite \)
\( -resize 150x150! icon_1.png -repage 150x150+0+0 \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) -geometry +150+150 -composite
output_ok.jpg
And result Image

But i think i need to examine all used operators as it seems first resize is not usefull.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert: parenthesis nested too deeply

Post by fmw42 »

try removing the +swap in my command
dovlan
Posts: 6
Joined: 2013-07-19T15:52:19-07:00
Authentication code: 6789

Re: convert: parenthesis nested too deeply

Post by dovlan »

Doesn't help.
What helps is removing entire +clone -background black -shadow 30x3+5+5 , but this would remove dropshadow, which is a required effect ( although in my example it already exists on images i am overlaying).
There might be a more simple way to do this, but I am constrained by time and knowledge to look for it.

Thank you for time spent on my problem.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert: parenthesis nested too deeply

Post by fmw42 »

Either of these commands work for me:

Original:
Image

convert -size 500x500 xc:lightskyblue \
\( \( sailboat2.png -resize 25% \) \( +clone -background black -shadow 30x3+5+5 \) \
+swap -background none -layers merge +repage \) \
-geometry +50+50 -compose over -composite \
\( \( sailboat2.png -resize 50% \) \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) \
-geometry +300+300 -compose over -composite sailboat2_comp.jpg

convert -size 500x500 xc:lightskyblue \
\( sailboat2.png -resize 25% \( +clone -background black -shadow 30x3+5+5 \) \
+swap -background none -layers merge +repage \) \
-geometry +50+50 -compose over -composite \
\( sailboat2.png -resize 50% \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) \
-geometry +300+300 -compose over -composite sailboat2_comp.jpg

Image
dovlan
Posts: 6
Joined: 2013-07-19T15:52:19-07:00
Authentication code: 6789

Re: convert: parenthesis nested too deeply

Post by dovlan »

Hi,
here it is

Image

Let me fill in some details . This operation is a part of larger script that assembles a short animation using elements like the image above, and adds effects like fade-in and dropshadow. The icon from the link already has dropshadow, but in some cases that will not be the case. In its full glory, my line would look like this

convert \( \( -size 300x300 xc:blue \) \
\( -resize 100x100! icon_1.png -repage 150x150+0+0 \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) -geometry +0+0 -composite \) \
\( -resize 100x100! icon_1.png -repage 150x150+0+0 \( +clone -background black -shadow 30x3+5+5 \) +swap -background none -layers merge +repage \) -channel Alpha -evaluate Multiply 0.5 -geometry +150+150 -composite \
output_example.jpg
This would add one icon on (0,0) with dropshadow and resize, and another on (150,150) with resize, dropshadow and modified transparency
Desired outcome
Image

TIA
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: convert: parenthesis nested too deeply

Post by fmw42 »

see my edited post above yours

My resulting command is similar to yours, but with a fewer parentheses and the correct IM syntax of loading the image before resizing. It won't matter here in your case in IM 6, but will fail for IM 7. You do not need the -repages. All they do is enlarge the canvas holding the resized icon.
dovlan
Posts: 6
Joined: 2013-07-19T15:52:19-07:00
Authentication code: 6789

Re: convert: parenthesis nested too deeply

Post by dovlan »

Thanks,

it helped a lot, since it eliminated nesting, and actually looks cleaner.

Thanks for the effort
Post Reply