ImageMagick添加带空格文本时返回非合规绘图基元错误排查
Let's break down what's happening here and how to fix it:
The Root Cause
You're hitting a known command-line parsing bug in ImageMagick 7.0.7-33. This version has an issue when handling -stroke combined with text that contains spaces (like "one two") via -annotate, label:, or caption:.
The bug makes ImageMagick misinterpret the space in your text as a separator between command arguments. Instead of treating the entire string as your caption, it splits it and tries to parse the part after the space (like "two") as a drawing primitive (hence the path or text error in the message).
Oddly enough, the drawing logic still runs despite the parsing error, which is why your image turns out correct—but the exit code 1 breaks script automation.
Fixes to Try
1. Upgrade ImageMagick (Recommended)
This bug was fixed in later releases of ImageMagick 7.x. The simplest solution is to upgrade to the latest stable version via Homebrew:
brew update && brew upgrade imagemagick
After upgrading, re-run your original command—it should execute without errors and return an exit code 0.
2. Workaround for the Current Version
If you can't upgrade right now, you can fix the parameter parsing by properly quoting your text string to prevent splitting:
- For
-annotate, wrap your caption in an extra set of quotes (escape them or use single quotes):convert -size 600x200 xc:none -coalesce -font Impact -pointsize 50 -fill white -stroke black -strokewidth 1 -gravity south -annotate 0 '"one two"' caption.png - If using
-draw, format the text command explicitly with quotes around the caption:convert -size 600x200 xc:none -coalesce -font Impact -pointsize 50 -fill white -stroke black -strokewidth 1 -gravity south -draw "text 0,0 'one two'" caption.png
This tells ImageMagick to treat the entire string as a single text value, avoiding the primitive parsing error.
Why the Image Still Generates
Even though you get an error message, the drawing pipeline in ImageMagick continues to run after the parsing hiccup. That's why your final image looks correct—but the exit code remains 1, which is why scripts fail to recognize it as a successful run.
内容的提问来源于stack exchange,提问作者jyurek

