command-lineimagemagickoverlayimagick

ImageMagick: How do I reuse one single image to overlay it multiple times


I would like to overlay the same image at different random positions multiple times (up to thousands of times) in ImageMagick with command-line.

convert -size 800x800 xc:transparent ^
-draw "image over 5,10 0,0 'overlay.png'" ^
-draw "image over 58,23 0,0 'overlay.png'" ^
-draw "image over 701,500 0,0 'overlay.png'" ^
....
PNG64:output.png

or

convert -size 800x800 xc:transparent ^
overlay.png -geometry +0+200 -composite ^
overlay.png -geometry +15+512 -composite ^
overlay.png -geometry +699+20 -composite ^
....
PNG64:output.png

work just fine!

But I have the feeling, ImageMagick is reloading the overlay.png every time from the disk, which seems very bad for performance.

Is there a better approach? For example, something with "-clone"?

Any help is much appreciated!


Solution

  • ImageMagick can read an image into a command and store it in a memory register to be accessed from memory during the run of the command. Here's a simple example...

    convert overlay.png -write mpr:img0 +delete ^
        -size 800x800 xc:transparent ^
        -draw "image over 5,10 0,0 mpr:img0" ^
        -draw "image over 58,23 0,0 mpr:img0" ^
        -draw "image over 701,500 0,0 mpr:img0" ^
    ....
    PNG64:output.png
    

    That reads the overlay image into the command and writes it to a memory register named "img0", (you can name it anything you want), then deletes it from the active command. Then, whenever it's needed it can be brought back into the command simply by referring to its name, "mpr:img0".