Page 1 of 1

Help with batch appending three series of images

Posted: 2015-06-08T09:41:20-07:00
by Beani
Hi all, I recently found out about ImageMagick and was appealed by its possibilities.

For my study I'm processing a lot of images that result from several measurements but I'm struggling to organize them in a proper way.
I have three images that I was to merge into one image in a 2x2 raster. However, the first picture has to be centered in the top row and the second and third image have to be at location 3 and 4 in the bottom row. I have a decent knowledge of Matlab scripting and thought it would be a simple task but it has already taken me several hours and I'm no step further. The main culprit is the batch processing. All images in the three series have serialized file names from 001 to 054 (001_VisNir, 001_Vis, 001_Nir, 002_VisNir, ..., 054_Vis, 054_Nir). I can combine them individually, but can't let the script convert the whole list.

Original pictures:
1:
Image
2:
Image
3:
Image

The results should be like:
Image

Preferable I would like to have the upper figure at 200% so that it fills the whole row, but I didn't get that to work.

My current script is:

Code: Select all

cd <directory>

convert ( [001-054]_VisNir.png +append ) -gravity center ( [001-054]_Vis.png [001-054]_Nir.png +append ) -background none -append [001-054].png
I'm having the feeling that [001-054] is not working to process the files one-by-one. I tried several "for" syntax, but that did not work either. This is probably a very simple question for most of the users of this forum but I tried and tried and looked at all possible FAQs and could not find a good explanation. This might be because English is not my mother language and I have difficulties finding the right terms for my problem. Also, excuse me for grammar and sentence errors.

I hope this is enough information for you guys and hope that you could help me a bit. A redirection to a clear guide/FAQ would also be sufficient!

This question is focused on a series of 54 measurements but I would like to apply it also in other sessions.

Re: Help with batch appending three series of images

Posted: 2015-06-08T10:22:18-07:00
by fmw42
Do I misunderstand your use of [001-054]? Is that a group of images or a label for one image? If the latter, then

Code: Select all

convert ( [001-054]_VisNir.png -resize 200% ) ( [001-054]_Vis.png [001-054]_Nir.png +append ) -gravity center -background none -append [001-054].png
I assume by the syntax, you are on Windows and using a current version of IM. Please always post your IM version and platform.

I am not a Windows user, but you may need to double the %. I do not know if that is needed in command line or bat script. But one of them needs the doubling. A Windows user should be able to clarify that, if need be.

Re: Help with batch appending three series of images

Posted: 2015-06-08T11:56:39-07:00
by Beani
Thanks for your response. First of all, sorry for not mentioning the IM version and platform. I was planning to do so but forgot it while writing down my problem.

I'm using the most current version of IM, ImageMagick-6.9.1-4-Q16-x86-dll.exe. I'm on Windows 7, 32 bit. I read about the % difference between UNIX and the Windows CMD, I thought that double %'s were necessary for the CMD.

With [001-054] I meant a series of images, so no label. I read somewhere that this syntax could be applied for a series but I'm constantly getting an error that the file "[001-054]_VisNir.png" (same for the others) cannot be found, so I assume this is wrong. I hoped I could avoid any FOR loops with this way of writing it down because I'm having a hard time understanding the syntax.

Thanks again for your response.

Re: Help with batch appending three series of images

Posted: 2015-06-08T14:09:46-07:00
by fmw42
You will need to write a loop over your image sequences and use convert. You can do that with a bat script or just a series of commands. A windows user will likely need to help, if you do not know how to do that. See http://www.imagemagick.org/Usage/windows/

Your command will read every image referenced into memory and will try to append every one together in the parens from the sequence you specified. If the images are large, you will run out of memory besides not getting what you want even if you do not run out of memory.

Re: Help with batch appending three series of images

Posted: 2015-06-09T00:10:16-07:00
by Beani
That's indeed the way I envisioned for the script to work. I looked at the windows page but I don't understand the initialization of the FOR loops. I am used to loops such as:

Code: Select all

for i = 1:54
	convert ...
end
I'm using a batch script. I tried removing the 0's in the file names so that my current code is:

Code: Select all

cd C:\Users\b.bakhuizen\Desktop\test3\Alles

convert ( ( [1-54]_VisNir.png +append -resize 200%%) -gravity center ( [1-54]_Vis.png [1-54]_Nir.png +append ) -background none -append ) ( [1-54]_total.png )
However, I'm getting the errors:
convert.exe: unable to open image `[1-54]_total.png': No such file or directory @ error/blob.c/OpenBlob/2695.
convert.exe: unable to open file `[1-54]_total.png' @ error/png.c/ReadPNGImage/3912.
convert.exe: unbalanced parenthesis `)' @ error/convert.c/ConvertImageCommand/3207.
Which suggest that the [1-54] notation neither works.

I don't think memory will be an issue as the images are quite small and I have enough RAM.

Re: Help with batch appending three series of images

Posted: 2015-06-09T01:01:43-07:00
by snibgo
The [1-54] notation reads all those images. You don't want that. You want to read only one. And it isn't a valid output.

Do it in a FOR loop.

Re: Help with batch appending three series of images

Posted: 2015-06-09T01:23:09-07:00
by Beani
Thanks for your response. So I have to ditch the [1-54] notation? Could anyone give an example what the FOR loop should loop like? I'm confused by the for loop syntax in the CMD.

It's probably something like (part pseudo code):

-----
for <wildcard i> do

convert ( <wildcard i>_VisNir.png +append -resize 200%% ) -gravity center ( <wildcard i>_Vis.png <wildcard i>_Nir.png +append ) -background none -append <wildcard i>.png

i++
-----

With i running from 001 till 054 or if it's easier 1 till 54.

Re: Help with batch appending three series of images

Posted: 2015-06-11T05:57:46-07:00
by Beani
Hi guys,

Anyone have an suggestion for my earlier post?

Is there despite 142 views nobody who knows anything about FOR loops in batch files in Windows?
Thanks for your response. So I have to ditch the [1-54] notation? Could anyone give an example what the FOR loop should loop like? I'm confused by the for loop syntax in the CMD.

It's probably something like (part pseudo code):

-----
for <wildcard i> do

convert ( <wildcard i>_VisNir.png +append -resize 200%% ) -gravity center ( <wildcard i>_Vis.png <wildcard i>_Nir.png +append ) -background none -append <wildcard i>.png

i++
-----

With i running from 001 till 054 or if it's easier 1 till 54.

Re: Help with batch appending three series of images

Posted: 2015-06-11T06:12:37-07:00
by snibgo
Have you tried typing "FOR /?"

This will show you the syntax of a FOR loop. Hint: you want the "/L" option.

Re: Help with batch appending three series of images

Posted: 2015-06-11T07:17:01-07:00
by Beani
Thanks, for some reason I did not yet think of that but it was helpful in multiple aspects.

I'm using G as the variable now in stead of i. I don't suspect it to make a lot of difference. When I run my current code, unfortunately I'm getting an error mentioning that the syntax of the task is not right. It seems to go wrong at the first line, e.g. "FOR /L %%G IN (1,1,54) ".

Code: Select all

cd <directory>

FOR /L %%G IN (1,1,54) 

DO

convert ( ( $$G_VisNir.png +append -resize 200%%) -gravity center ( $$G_Vis.png $$i_Nir.png +append ) -background none -append ) ( $$G_total.png )

done

Re: Help with batch appending three series of images

Posted: 2015-06-11T08:24:50-07:00
by snibgo
Put "DO" on the same line as "FOR".

You need an open parenthesis after "DO", and a matching close at the end.

On the other hand, you have at least one pair of parentheses too many within the "convert" command.

Re: Help with batch appending three series of images

Posted: 2015-06-12T05:00:00-07:00
by Beani
Thanks very much. I'm a lot further now. When looking more focused at my code I spotted a severe problem as I named all of my variables in the 'convert' line $$G in stead of %%G. Also, my parentheses were not equal indeed. I removed all of whom I thought are unnecessary.

My current code:

Code: Select all

FOR /L %%i IN (1,1,54) DO (
convert %%i_VisNir.png -resize 200%% ( %%i_Vis.png %%i_Nir.png +append ) -append -background none %%i_Total.png
)
I have on remaining problem however, the CMD gives me an error that '-append' is not expected. However, when I'm running the convert syntax without the FOR loop, there is no problem and the conversion works great and just like I want. The syntax is exactly the same but the '%%i' is changed for and '1'.

Code: Select all

convert 1_VisNir.png -resize 200%% ( 1_Vis.png 1_Nir.png +append ) -append -background none 1_total.png
I do not understand why it behaves different. The FOR loop works, which I tested with:

Code: Select all

FOR /L %%i IN (1,1,54) DO (
echo %%i
)

Re: Help with batch appending three series of images

Posted: 2015-06-12T05:29:44-07:00
by snibgo
You need to escape each parenthesis within convert: ^( and ^).

Re: Help with batch appending three series of images

Posted: 2015-06-12T06:19:07-07:00
by Beani
That was the trick! I read somewhere that escaping wasn't necessary in the Windows CMD.
Windows DOS scripts do not require parenthesis to be escaped with backslash. See Windows DOS Scripts for this and other differences to linux scripting
However, after reading ImageMagick v6 Examples -- Usage under Windows it became clear that you are totally right.
All reserved shell characters which are not in double quotes must be escaped with a '^' (caret or circumflex) if used in a literal sense (i.e. not fulfilling their usual purpose). These reserved shell characters are: '&', '|', '(', ')', '<', '>', '^'.
Thanks for your help, now I can continue the analysis of my results. As a bonus my final product:
Image

It looks so simple, I can't understand I was stuck at this for a couple of days :shock: .

I'll see if I can reference to this forum and the software in my report! I expect to use the software more regularly for batch processing!

Final code for future reference:

Code: Select all

FOR /L %%i IN (1,1,54) DO (
convert ^( %%i_VisNir.png -resize 200%% ^) ^( %%i_Vis.png %%i_Nir.png +append ^) -append -background none %%i_Total.png
)