Search code examples
imageimagemagickmosaic

ImageMagick crop resize multiple files mosaic at once


I have like 1000 images in defferent sizes. Is it possible in one command to crop and resize them then do a mosaic from them. I have tryied this way it's resizing the images but the -page option is not working correctly it's not getting the resized dimension, but the origlinal one.

Here is my example it doesn't have crop in it, since I realy don't understand very well how I can apply multy operations on one file and then move to another one.

convert \
-page +0+0 img1.png -resize 50x50 \
-page +50+0 img2.png -resize 50x50 \
-page +100+0 img3.png -resize 50x50 \
....
-mosaic mosaic.png

So, is it possible to do multiple operations in file and then move to another one ect. And then create a mosaic of them in one shot. And is it actually good idea to do all this in one shot, or first I have to prepare each small image and then do the mosaic. I mean If I have really big collection of images I probably will run out of RAM, so in this case I could do a small parts of the mosaic and then combain them all thogether.

But the second question still appears is it not better to prepare all small tiles first and then do the mosaic.

Thanks for any help.


Solution

  • You probably want something like the following:

    #!/bin/bash
    # Avoid problems if there are no JPGS, or no PNG files, and allow JPG or jpg, i.e. case-insensitive 
    shopt -s nullglob nocaseglob
    
    # Loop through all JPGs and all PNGs in current directory
    for f in *.png *.jpg; do
        convert "$f" -resize 50x50 -crop 30x30+0+0 +repage miff:-
    done | montage -tile 10x -geometry +0+0 miff:- result.png
    

    That means you run one convert for each image to get it to the right size and shape and send that to the MIFF: output stream. At the end of the loop, you start a montage command which collects files from the MIFF: and places them in rows of 10 images across the page (-tile 10x) with no spaces in between (-geometry +0+0).

    If you want 30 images down the page, rather than 10 rows across it, replace 10x with x30.

    P.S. Don't try to add any debug statements to stdout inside the loop, as they will be passed into montage which will confuse it!