2

I'm currently trying to create an image with a very large number of other images using ImageMagick. Each image has its own crop values, and placement within the larger image. Rather than a large number of read/writes to the output image, I'd like to send the command all at once, but it is far too large to pass via the command line.

I've found references to a seemingly undocumented feature of ImageMagick, scripts. However I can't seem to figure them out. Take this very simple command from the docs:

magick -size 50x50 xc:skyblue an_image.png -composite output.png

This creates a blue square, "pastes" an_image.png onto it, and saves it as output.png. However I'm struggling to recreate even this with a script:

script.mgk:

-size 50x50 xc:skyblue an_image.png -composite output.png

command:

magick -script script.mgk

The above outputs the error:

magick: unable to open image 'output.png': No such file or directory @ error/blob.c/OpenBlob/3569.

Trying additional lines yields similar errors. Though if an_image.png doesn't exist (for example) it will instead print an error saying that that file couldn't be found.

Any help is greatly appreciated!

stuntboots
  • 71
  • 8

1 Answers1

2

The scripting feature is documented here.

There are a few examples on StackOverflow too, e.g. here , here and others.

Specifically, on the command line, the last item is assumed to be a filename and the results get written there. When using the script interface, however, there is no such assumption, and you need -write filename if you want a file created. You should be able to see that in the cited examples hopefully.

So, in concrete terms, change your script to:

-size 50x50 xc:skyblue
an_image.png -composite
-write output.png
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • This is fantastic, thanks very much! (And thanks even more for ImageMagick itself!) For my own curiousity, adding an image entry like: `an_image.png -crop 20x20+10+5 -composite` Causes an error, and adding the -crop before the file name crops the output image. I realised it's better to do this instead: `an_image.png[20x20+10+5] -composite`. But I was wondering what the correct syntax would be for the former, to try and understand ImageMagick itself a bit better, haha. – stuntboots May 30 '23 at 07:53
  • It's generally a good idea to add `+repage` immediately after cropping, trimming, resizing so maybe see if that does the trick. Version 7 of **ImageMagick** is miles more powerful and expressive than v6 and as a result it is more picky about the order of its parameters - for example you can't use `-crop` before loading an image like you used to be able to. – Mark Setchell May 30 '23 at 08:29