Search code examples
batch-filecmdimagemagicktiffimagemagick-convert

How to merge files with partial matches of filename to numeric range in ImageMagick


There are a list of numbered files:

out0001.tif, out0002.tif, ..., out0100.tif, out0101.tif, ..., out2300.tif

And I'd like to merged them into layered tif (using ImageMagick convert) that should contain 100 layers. E. g.

merged_1-100.tif, merged_100-200.tif, ...

When I run the following cmd, it try to group all tifs in one huge, but I need only first hundred:

convert out00??.tif out0100.tif -adjoin merged_1-100.tif

What I need to modify in the command?


Solution

  • I found a solution using a batch file.

    @echo off
    
    set file_list=
    set /a start_index=1
    set /a finish_index=100
    
    for /l %%G in (%start_index%,1,%finish_index%) do call :make-filelist %%G
    
    rem Check
    echo %file_list%
    
    pause
    
    rem Make conversion
    convert %file_list% -adjoin merged_%start_index%-%finish_index%.tif
    
    goto :eof
    
    
    :make-filelist
        rem Add leading zeros
        set "fn=0000%1"
        set "fn=%fn:~-4%"
        
        rem Add filename to list of files
        set "file_list=%file_list%out%fn%.tif "
        goto :eof
    

    It allows using any range of file indexes. To do so it is necessary to change start_index and finish_index.