How to join images in a 2x2 grid using "convert"

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

How to join images in a 2x2 grid using "convert"

Post by T2ioTD »

Hi All

I am using this batch script extensively to collates images side by side:

SETLOCAL EnableDelayedExpansion
SET IMCONV="%PROGRAMFILES%\ImageMagick-7.0.4-Q16\Convert"

SET COUNT=0
setlocal ENABLEDELAYEDEXPANSION
FOR /R %%T IN (*.jpg) DO (
SET /A COUNT=!COUNT!+1
REN %%T A!COUNT!.jpg
)
:EOF

@echo off
set count=0
for %%x in (*.jpg) do set /a count+=1
set /a demi=%count% / 2

FOR /L %%x IN (1,2,%count%) DO (
set /a y = %%x + 1
%IMCONV% A%%x.jpg A!y!.jpg +append B%%x.jpg
)

endlocal

However I would like to collate also images in a grid of 2x2, and I do not know how.

PS. another mini problem in my script is that is it is unable to renumber correctly images if they start with a digit, but this may be for another time..
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to join images in a 2x2 grid using "convert"

Post by snibgo »

A convert command to append 4 images in a 2x2 grid is (Windows BAT syntax):

Code: Select all

convert ^
  ( a.jpg b.jpg +append ) ^
  ( c.jpg d.jpg +append ) ^
  -append ^
  out.jpg
When inside a "for (...)" loop, you need to escape the parentheses or quote them:

Code: Select all

convert ^
  ^( a.jpg b.jpg +append ^) ^
  ^( c.jpg d.jpg +append ^) ^
  -append ^
  out.jpg
snibgo's IM pages: im.snibgo.com
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: How to join images in a 2x2 grid using "convert"

Post by T2ioTD »

Snibgo, Inspired by your answer, and trying to incorporate it in my humble program, I manage no better than:

SETLOCAL EnableDelayedExpansion
SET IMCONV="%PROGRAMFILES%\ImageMagick-7.0.4-Q16\Convert"



SET COUNT=0
setlocal ENABLEDELAYEDEXPANSION
FOR /R %%T IN (*.jpg) DO (
SET /A COUNT=!COUNT!+1
REN %%T A!COUNT!.jpg
)
:EOF

@echo off
set count=0
for %%x in (*.jpg) do set /a count+=1
set /a demi=%count% / 2

FOR /L %%x IN (1,4,%count%) DO (
set /a y = %%x + 1
set /a w = %%x + 2
set /a z = %%x + 3
%IMCONV% A%%x.jpg A!y!.jpg +append
A!w!.jpg A!x!.jpg +append
B%%x.jpg +append
)
endlocal


However it is making an error... Could you help me please?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to join images in a 2x2 grid using "convert"

Post by snibgo »

Your convert command is very different to mine.
snibgo's IM pages: im.snibgo.com
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: How to join images in a 2x2 grid using "convert"

Post by T2ioTD »

The fact that I renamed Convert into IMCONV in the line: SET IMCONV="%PROGRAMFILES%\ImageMagick-7.0.4-Q16\Convert"
was been advised by IM itself so that there will be no conflict with Windows System own Convert operation...
Please disregard this line and provide your own lines of codes as you would normally do...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to join images in a 2x2 grid using "convert"

Post by snibgo »

What error message do you get? Some errors are obvious:

Your convert has 5 inputs and no output.

You have three "+append" instead of two "+append and one "-append"

You have no parentheses.

Most importantly, you have no line continuation characters.
snibgo's IM pages: im.snibgo.com
T2ioTD
Posts: 12
Joined: 2016-09-04T09:31:38-07:00
Authentication code: 1151

Re: How to join images in a 2x2 grid using "convert"

Post by T2ioTD »

Success! Based on Snibgo help, I managed the successful:


SETLOCAL EnableDelayedExpansion
SET IMCONV="%PROGRAMFILES%\ImageMagick-7.0.4-Q16\Convert"



SET COUNT=0
setlocal ENABLEDELAYEDEXPANSION
FOR /R %%T IN (*.jpg) DO (
SET /A COUNT=!COUNT!+1
REN %%T A!COUNT!.jpg
)
:EOF




set count=0
for %%x in (*.jpg) do set /a count+=1

FOR /L %%x IN (1,4,%count%) DO (
set /a y = %%x + 1
set /a w = %%x + 2
set /a z = %%x + 3

%IMCONV% ^
^( A%%x.jpg A!y!.jpg +append ^) ^
^( A!w!.jpg A!z!.jpg +append ^) ^
-append ^
B%%x.jpg

)

endlocal
Post Reply