Page 1 of 1

Combine only first 6 images of folder (limit output to single file)

Posted: 2014-11-10T13:55:48-07:00
by jdev21
Hello,
I am looking to create an index page for each of my folders that contain images. I would like on the index page to be the first 6 images of the directory combined to a 1200x1000 (3 columns 2 rows) image. I am trying to do this in windows prompt. Is it possible?

I have found that I can use this:

Code: Select all

montage *.jpg -geometry 400x500 -background black -tile 3x2 out.jpg
Unfortunately, this will create multiple out.jpg files. I only want 1 single file and then for it to stop afterwards.

I have found this nifty script online to catch the first 6 images of the folder but cannot figure out how to incorporate it into a batch script for use with imagemagick/montage.

Code: Select all

@echo off
setlocal
set /a "n=0, limit=6"
>"testfile1.txt" (
  for /f "eol=: delims=" %%F in ('dir /on /b *.*') do (
    echo %%F
    2>nul set /a "n+=1, 1/(limit-n)"||goto :break
  )
)
:break
Any advice would be greatly appreciated!

Re: Combine only first 6 images of folder (limit output to single file)

Posted: 2014-11-10T15:17:18-07:00
by snibgo
The script writes the names of the first 6 files in the directory to the new text file testfile1.txt. You can use this in a montage instead of the input filename, by preceding it with "@".

Code: Select all

@echo off
setlocal
set /a "n=0, limit=6"
>"testfile1.txt" (
  for /f "eol=: delims=" %%F in ('dir /on /b *.jpg') do (
    echo %%F
    2>nul set /a "n+=1, 1/(limit-n)"||goto :break
  )
)
:break

type testfile1.txt

%IM%montage @testfile1.txt -geometry 400x500 -background black -tile 3x2 zzzout.jpg
I have changed the "dir" to search for just jpg files. I've changed the output file to (probably) not be in the first 6.

The "type" just lists the files that will be montaged.

Re: Combine only first 6 images of folder (limit output to single file)

Posted: 2014-11-10T18:42:08-07:00
by jdev21
Thank you!

Do you know if it would be possible to get this to run through all of this to run through all subfolders of current so there is an index sheet created for every one?

Re: Combine only first 6 images of folder (limit output to single file)

Posted: 2014-11-10T21:32:54-07:00
by snibgo
Wrap most of the script in a FOR loop. Type "help for" for help.

Re: Combine only first 6 images of folder (limit output to single file)

Posted: 2014-11-11T09:11:38-07:00
by jdev21
Thank you for all of your help so far.

I have been trying to play with a for loop but cannot seem to get it to work properly. Any advice on what I am doing wrong?
Here is what I have so far:

Code: Select all

@echo off
setlocal
for /R %%G in (.) do (
set /a "n=0, limit=6"
>"testfile1.txt" (
  for /f "eol=: delims=" %%F in ('dir /on /b *.jpg') do (
    echo %%F
    2>nul set /a "n+=1, 1/(limit-n)"||goto :break
  )
)
:break

type testfile1.txt

%IM%montage @testfile1.txt -geometry 400x500 -background black -tile 3x2 zzzout.jpg
)
Is it possible to nest the for command?
When running as-is, it will run the batch file twice in the same parent directory even though there are 3 subdirectories which it should run through.

Re: Combine only first 6 images of folder (limit output to single file)

Posted: 2014-11-11T19:09:22-07:00
by snibgo
You've got a variable, %%G, which takes successive values of the directory names. Good. But you don't use this at all.

You can change the dir to use %%G:

Code: Select all

dir /on /b %%G\*.jpg
Then you also need %%G in the echo.

Nesting FOR loops can get messy. I often call a subroutine, like this:

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /R %%G in (.) do call :DoOneDir %%G
rem for /R %%G in (.) do echo %%G
goto :eof

:DoOneDir
rem goto break
set /a "n=0, limit=6"
>"testfile1.txt" (
  for /f "eol=: delims=" %%F in ('dir /on /b %1\*.jpg') do (
    echo %1\%%F
    2>nul set /a "n+=1, 1/(limit-n)"||goto :break
  )
)
:break

type testfile1.txt

%IM%montage @testfile1.txt -geometry 400x500 -background black -tile 3x2 %1\zzzout.jpg
This passes %%G as a parameter, so the subroutine uses %1.

However, this is now all about DOS BAT scripting, and not specific to ImageMagick. There are better forums to discuss scripting.