imagemagick montage file name formatting for top bottom column order

93 views Asked by At

after trying like 8 different software for montage I finally found Imagemagick that does it perfectly and super quckly, the issue however is that I can only get one column of tiles to show up, but more than 1 column and the picture looks divided up. I dont know how to tell software to create the montage with each column whenever a file name has ascended by one number in the file names.

I have following file naming: [0-68]_[0-98].jpg. So for an example the first file I have 0_0.jpg that is top left of the big image, 0_68.jpg is the first column of the entire image. So then 98_68.jpg is my last file for grid that is bottom right. So my grid is 99 columns and 69 rows, with 6,831 (69x99) files in a folder.

I am able to create 1 full column using this code

magick.exe montage 0_%d.jpg[0-68] -geometry +0+0 -tile 1x69 all.png

But I don't know how to manipulate file naming so that it creates a montage with first column with 0_[0-68].jpg files, second column 1_[0-68].jpg files, etc until it finishes the comntage with the 99th column going 98_[0-68].jpg .

Thank you

I am able to create 1 full column using this code

magick.exe montage 0_%d.jpg[0-68] -geometry +0+0 -tile 1x69 all.png
1

There are 1 answers

0
Mark Setchell On

If you have 6,800 images and you read a load in, append them into columns and write them out again and keep repeating that till you have all the columns and then read them all in again and append those across the page, you will be there all day going backwards and forwards to disk.

I think you'd be better off making a script that makes a single invocation of ImageMagick that does what you want. Let's take a simple example, of laying out 3 images across the page side-by-side. At the command-line that would be:

magick 0.png 1.png 2.png +append result.png

enter image description here

You can do that exact same thing in a script. So, make a text file called script.mgk that contains:

0.png 1.png 2.png +append -write result.png

And now run it with:

magick -script script.mgk

and the result is the same as above. You can use .txt as an extension (as opposed to .mgk) if that makes life easier with Windows.


Now let's do something more akin to what you actually want. Take some images and append down the page (each line of the script producing a column) then append the columns across the page:

Under bash that would be:

magick                            \
  \( 0.png 1.png 2.png -append \) \
  \( 3.png 4.png 5.png -append \) \
  \( 6.png 7.png 8.png -append \) \
  +append -write result.png

Under Windows, something more like:

magick ^
( 0.png 1.png 2.png -append ) ^
( 3.png 4.png 5.png -append ) ^
( 6.png 7.png 8.png -append ) ^
+append -write result.png

but if you use a script, the advantage is that you don't need to escape the parentheses, or add line-continuations and you can select the files you want to append in a column with a simple DIR /B *_0.png style of command. So your script for that becomes:

( 0.png 1.png 2.png -append )
( 3.png 4.png 5.png -append )
( 6.png 7.png 8.png -append )
+append
-write result.png

which you run in the same way:

magick -script script.mgk

That gets you this:

enter image description here


Now, the layout of the script file is flexible, so you can create it in this form and get the exact same results:

( 
0.png 
1.png 
2.png 
-append
)
(
3.png
4.png
5.png
-append
)
(
6.png
7.png
8.png
-append
)
+append -write result.png

So, I am envisaging you can make life easy for yourself by building the script in Windows CMD32.EXE (which I never use) along these lines:

ECHO "("          > script.mgk
DIR /B *_0*.png  >> script.mgk
ECHO "-append"   >> script.mgk
ECHO ")"         >> script.mgk
ECHO "("         >> script.mgk
DIR /B *_1.png   >> script.mgk
...
...

The first line, with a single > creates the first line of the script, while subsequent lines with double >> append additional lines to the end.

Or, hopefully you can do all the writing/appending in a single redirection using a compound command inside parentheses:

( ECHO "("      
  DIR /B *_0*.png
  ECHO "-append"
  ECHO ")"
  ECHO "("
  DIR /B *_1.png
) > SCRIPT.MGK

Either way, the key point is that you can use the wildcards in the DIR command to select whichever files you want for each column.


By the way, I made all the little numbered tiles (under bash on macOS) like this:

for ((i=0;i<10;i++)) ; do 
   magick -size 64x64 -background navy -fill white -gravity center caption:"$i" "$i.png"
done