imagemagickimagemagick-montage

Create fixed-size montage of images with missing files


Setting

Suppose we have a list of N elements of which an element can either be a path to an image (e.g. a.jpg) or NULL indicating that a file is missing.

Example (N = 6): a.jpg,NULL,c.jpg,NULL,NULL,f.jpg

All mentioned images (a.jpg, c.jpg, f.jpg) are guaranteed to have the same resolution.

Task

Create a fixed-width montage (e.g. out.jpg) in which NULL values are replaced with black images whose resolutions are consistent with the common resolution of a.jpg, c.jpg, f.jpg. I would like to abstain from creating an actual black.jpg and would prefer to create the image on-the-fly as needed.


Solution

  • Using ImageMagick's "montage" command, if your images are known dimensions so you can include that in the command, and if you can generate a text file "list.txt" of the image files and put "xc:black" on each line that has no image like this...

    image00.png
    image01.png
    image02.png
    image03.png
    image04.png
    xc:black
    image06.png
    image07.png
    xc:black
    xc:black
    image10.png
    image11.png
    

    You can run the ImageMagick "montage" command something like this...

    magick montage @list.txt -tile 3x4 -geometry 160x160+3+3! out.png
    

    The "@" in front of the name of the text file tells IM to read the input images from there. The "-tile" describes how many columns and rows will be in the result. The "-geometry" setting is where you put the dimensions of the images and the spacing between columns and rows. The "xc:black" images are single black pixels, but the exclamation point forces them to the W and H dimensions in the "-geometry" argument.

    That will create black images everywhere you have "xc:black" in the list. If you want to fill between the spaces with black also, add "-background black" to the command.

    That works for me with IMv7 and "magick montage ..." For IMv6 you just use "montage". I'm pretty sure everything else about the command would work the same way.