1

Is there a way to replace a sequence of Magick commands like:

magick folder1/1.jpg[80x100] folder2/1.jpg
magick folder3/2.jpg[80x100] folder4/2.jpg
...
magick folder9/xyz.jpg[80x100] folder10/xyz.jpg

into a single invocation of magick with all the commands in a single file, as in:

magick @command.lst

where "command.lst" is a text file that contains:

folder1/1.jpg[80x100] folder2/1.jpg
folder3/2.jpg[80x100] folder4/2.jpg
...
folder9/xyz.jpg[80x100] folder10/xyz.jpg

Thanks! Dan

Dan Lewis
  • 93
  • 8
  • You can also make a plain shell script (.bat, .sh, depending on your OS) with multiple `magick` commands. You can also have [one single magick command that applies to a pattern, and output names "computed" from the input names](https://stackoverflow.com/a/27784462/6378557). – xenoid Apr 08 '23 at 22:02
  • Thanks, but unfortunately, those suggestions don't work for me. The first one means reloading magick for every conversion. I'm invoking it from within a C program, so I was trying to avoid reloading it for every conversion. The second one doesn't help either because the folders containing the files may vary from one command to the next. – Dan Lewis Apr 09 '23 at 02:44
  • In modern OSes, the reload of the executable is negligible compared to loading/writing the images, the code is in cache. – xenoid Apr 09 '23 at 06:14

1 Answers1

2

You can put a bunch of commands in a script similar to this and call it script.mgk:

a.jpg -resize 10x10 -write c.jpg -delete 0
b.jpg -resize 10x10 -write d.jpg -delete 0

Then invoke it with:

magick -script script.mgk

If you don't actually want to create a physical file on disk, you can create the commands dynamically and pipe them into ImageMagick's stdin like this:

{ 
   echo "-size 64x48 xc:black -write a.jpg +delete"
   echo "-size 32x24 xc:blue  -write b.png"
} | magick -script -

Or, you can use a bash "heredoc" like this:

cat <<EOF | magick -script -
-size 64x48 xc:black -write a.jpg +delete
-size 32x24 xc:blue  -write b.png
EOF
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432