2
votes

I have a folder containing 4 sub-folders, and each sub-folder contains 4 images (for a total of 16 images). I want to combine these 16 images into 1 image.

Example of structure:

0/0.png
0/1.png
...
1/0.png
1/1.png
...
3/3.png

This is how the images are combined into one image:

Each column represents a sub-folder.

I have +30 of these folders, and that's why I want to create a script instead of doing it manually in Photoshop.

I downloaded ImageMagick for Windows and tried to create a batch script that I could run.

I tried this:

cd %cd%
for /r /d %%a in (*.png) do montage -tile 5x4 "result.png"

This doesn't work (no image, nor error in CMD). Please note that I am completely new to both ImageMagick and creating .bat files in Windows.

So, how can modify my script so it goes through all the sub-folders and creates/convert a single montage/tile from all the images in the sub-folders?

2
ImageMagick montage does not traverse subdirectories. You will have use access all images in each subdirectory and feed that to montage. Sorry, I do not code in Windows. If image 1-4 is in the same subdirectory, you will need to rearrange images before feeding to montage or transpose the image after montage. In what way does it not work?fmw42
You also need to specify if there is any preference as to the order of those images with respect to the final montage, i.e. which one goes where?Compo
@fmw42 It does not produce an image at all, but I don't get an error either2by
magick 0/*.png -append ( 1/*.png -append ) ( 2/*.png -append ) ( 3/*.png -append ) +append result.pngMark Setchell
@Marck Setchell. Your code is Unix not Windows. Change your slashes and use magick.exe. Good idea to just use append. magick.exe ( 0\*.png -append ) ( 1\*.png -append ) ( 2\*.png -append ) ( 3\*.png -append ) +append result.pngfmw42

2 Answers

2
votes

I do not know Windows scripting syntax. But if you loop over each directory of your 30 directories and run the following ImageMagick command from Mark Setchell converted to Windows slashes, that should work.

magick.exe ( 0\*.png -append ) ( 1\*.png -append ) ( 2\*.png -append ) ( 3\*.png -append ) +append result.png


Since I do not know Windows .bat scripting, it is possible that the answer from Gerhard Barnard may do just that.

0
votes

Your example loops through all images, but you are doing it in the current dir only, not sub directories. The command also does not even use the metavariable %%a anywhere and if it did, it performs the do function to each file seperately.

You could just loop through the images using dir, get each and append them using set /p in batch. so no matter how many images you have, it should loop through them.. Here I just store the result in a tmp file to use it in another loop, but there are many ways.

@echo off
set "myt=%temp%out.tmp"
for /f %%i in ('dir /b /a-d /S *.png') do echo| set /p = ("%%~fi" -append)>>"%myt%"
for /f "delims=" %%b in (%myt%) do magick.exe %%b +append result.png
del /Q "%myt%">nul

NOTE I am not too farmiliar with magick so not sure if the double quotes will cause issues, however I doubt it as paths with spaces would should cause issues.