How to use stdin with caption in ImageMagick

2019-02-20 02:33发布

问题:

My understanding from reading the ImageMagick documentation regarding text, is that the @- notation reads the contents of standard input.

As such, this should be a fairly straight forward way to render Hello World.

printf "Hello\nWorld" | 
convert \
  -size 1280x100 \
  -background '#0000FF10' \
  -density 90 \
  -gravity Center \
  -fill black \
  -font Helvetica \
  caption:@- \
  test.png

On OS X 10.11.5 via HomeBrew, this works, using convert Version: ImageMagick 6.9.4-3 Q16 x86_64 2016-05-20.

However on Ubuntu 16.04 LTS, the identical command does not work, using convert Version: ImageMagick 6.8.9-9 Q16 x86_64 2016-06-01. In fact, it renders the stdin operator, literally.

The only thing I was able to find that remotely looked like this problem on Google was this article, dated back in Oct 2015 in which ImageMagick 6.9.2-5 Beta was patched to fix a similar problem.

QUESTION: Am I not escaping it properly, is there really a problem in ImageMagick, or is my Linux Distro picking up a historical version of ImageMagick with the bug and I need to build from source?


Much Later After Many Experiments

SOLVED ...? Built ImageMagick 7.0.2 from source on the Ubuntu box and the above command worked as desired. Was there a better solution?

回答1:

No need to build from source. Just replace @- with "`tee`" :

printf "Hello\nWorld" | 
convert \
  -size 1280x100 \
  -background '#0000FF10' \
  -density 90 \
  -gravity Center \
  -fill black \
  -font Helvetica \
  caption:"`tee`" \
  test.png

`tee` will execute first and 'process' stdin before completing the convert command.



回答2:

I suspect it is down to differences in your policy.xml file. There were recent warnings about IM security vulnerabilities (detailed here) and I guess the policy.xml file on one of your servers has been made secure and not the other. The affected line in that file is:

<!-- <policy domain="path" rights="none" pattern="@*" /> -->

Clarifications contributed by question owner:

  • The policy file's location is /etc/ImageMagick-6/policy.xml
  • The default state in the package distribution is UNcommented out.
  • The recommendation posed here is to comment it out.
  • This solution did not work for the question owner; your mileage may vary,

Further clarification by Mark:

The location of the policy.xml file is not always /etc/ImageMagick-6, it is different on different systems - for example on OS X it is under /usr/local/Cellar/imagemagick....

The sure way to find the policy.xml file is to run the following command and realise that ImageMagick expects the policy.xml file to be in the same directory as the delegates.xml and coder.xml:

 convert -debug configure logo: null: 2>&1 | grep -Ei "Searching|Loading"