Search code examples
bashimage-processingimagemagickmogrify

How do you batch process drawing in imagemagick 7?


I have made a command that works with magick 7. It uses the new %[fx] and draw syntax

magick input.png \
 -stroke black -strokewidth 3 \
  -draw "line 0,0 20,0" \
  -draw "line 0,0 0,20" \
  -draw "line 0,%[fx:h] 20,%[fx:h]" \
  -draw "line 0,%[fx:h] 0,%[fx:h-20]" \
  -draw "line %[fx:w],0 %[fx:w-20],0" \
  -draw "line %[fx:w],0 %[fx:w],20" \
  -draw "line %[fx:w],%[fx:h] %[fx:w-20],%[fx:h]" \
  -draw "line %[fx:w],%[fx:h] %[fx:w],%[fx:h-20]" \
output.png

Unfortunately it does not work for batch processing with magick mogrify

magick mogrify -stroke black -strokewidth 3 \
  -draw "line 0,0 20,0" \
  -draw "line 0,0 0,20" \
  -draw "line 0,%[fx:h] 20,%[fx:h]" \
  -draw "line 0,%[fx:h] 0,%[fx:h-20]" \
  -draw "line %[fx:w],0 %[fx:w-20],0" \
  -draw "line %[fx:w],0 %[fx:w],20" \
  -draw "line %[fx:w],%[fx:h] %[fx:w-20],%[fx:h]" \
  -draw "line %[fx:w],%[fx:h] %[fx:w],%[fx:h-20]" \
-- *.png

and gives an error

mogrify: non-conforming drawing primitive definition `%' @ error/draw.c/RenderMVGContent/4059.

I could use the v6 syntax but this does not seem like it should be necessary.


Solution

  • The issue is that mogrify in Imagemagick is old IM 6 structure and is just called when using IM 7 magick mogrify. It did not have all the upgrades put in to IM 7 magick. One of the big things in IM 7 magick is the ability to do fx computations inline. But it was not put into mogrify. You can create a number of variables for each fx: in separate commands and use the variables in your magick mogrify command to do the draw.

    In Unix syntax, this would be:

    infile="input.png"
    declare `magick "$infile" -format "ww=%w\n hh=%h\n www=%[fx:w-20]\n hhh=%[fx:h-20]\n" info:`
    magick "$infile" -stroke black -strokewidth 3 \
      -draw "line 0,0 20,0" \
      -draw "line 0,0 0,20" \
      -draw "line 0,${hh} 20,${hh}" \
      -draw "line 0,${hh} 0,${hhh}" \
      -draw "line ${ww},0 ${www},0" \
      -draw "line ${ww},0 ${ww},20" \
      -draw "line ${ww},${hh} ${hhh},${hh}" \
      -draw "line ${ww},${hh} ${ww},${hhh}" \
    output.png