Page 1 of 1

append left and right page images

Posted: 2017-11-29T06:11:33-07:00
by StevePM
I am trying to append a series of left and right opposing page images 001_L.jpg 002_R.jpg and so on. I can create a large batch file like

magick 001_L.jpg 002_R.jpg +append output/page1.jpg
magick 003_L.jpg 004_R.jpg +append output/page2.jpg
......etc

This works and puts the joined images into sub folder 'output' , but as i need to do this for many types of scanned book pages I thought there has to be a simpler method to loop around the all the files using a For loop but i'm not familiar with the use of variables etc and how this would work incrementing the filenames correctly.

Any suggestions

Thanks

Re: append left and right page images

Posted: 2017-11-29T07:51:41-07:00
by snibgo
The for loop will be in the script language you use. You haven't said what language that is, eg bash or Windows BAT.

Re: append left and right page images

Posted: 2017-11-29T08:03:35-07:00
by StevePM
hi i'm Running Windows 10, so I guess a .bat file

Re: append left and right page images

Posted: 2017-11-29T09:21:20-07:00
by snibgo
The messy part is the leading zeros. I add two zeros to the left of the string, and take the final three characters.

Code: Select all

for /L %%I in (1,1,6) do (
  echo %%I
  set /A LZL=2*%%I-1
  set LZL=00!LZL!
  set LZL=!LZL:~-3!
  echo !LZL!

  set /A LZR=2*%%I
  set LZR=00!LZR!
  set LZR=!LZR:~-3!
  echo !LZL! !LZR!

  echo magick !LZL!_L.jpg !LZR!_R.jpg +append output/page%%I.jpg
)
To actually run the magick command, remove the "echo" in that line. The other "echo" commands are so you see what is happening, and they can be removed.

If you don't have 6 pairs, change "6" in the first line.

Re: append left and right page images

Posted: 2017-11-29T09:47:36-07:00
by GeeMack
StevePM wrote: 2017-11-29T06:11:33-07:00 I am trying to append a series of left and right opposing page images 001_L.jpg 002_R.jpg and so on. I can create a large batch file like

magick 001_L.jpg 002_R.jpg +append output/page1.jpg
magick 003_L.jpg 004_R.jpg +append output/page2.jpg
......etc
If your images are all the same size and if your system has enough memory to read all the images into a single command, something like this should give you the result you're looking for...

Code: Select all

magick *L.jpg -extent %[fx:w*2]x0 null: *R.jpg -gravity east -layers composite output/page%02d.jpg
That reads in the left side images, extends their canvas to double the width toward the right, then composites the right side images over that extended area of their left side partners. The outputs will be named "pageNN.jpg", with the NN being numbering with a leading zero to two places and starting with "00".

This assumes, of course, that each page has a mate and that the sequence for reading in the left side images correlates to the sequence for the right side images.

A little tweaking may be required if each image doesn't have a left/right mate.

To work a command like that into a BAT script you'd need to change the single percent signs "%" into doubles "%%".

If the file sizes are especially large, it may tax the memory on your system and a "for" loop would probably be a better approach.

Edited to add: If you want the output file numbers to start with "01" you can add "-scene 1" before the output file name at the end of the command.

Re: append left and right page images

Posted: 2017-11-29T10:02:36-07:00
by snibgo
As always, a smart solution from GeeMack. It assumes the left and right images in each pair have the same width and height. If that isn't true, it won't be the same as "+append".

Re: append left and right page images

Posted: 2017-11-29T10:12:17-07:00
by StevePM
Hi thanks for the suggestion which I've tried, ( I just put the script in a .bat file and ran the bat file) but couldn't see on screen what was happening (even with the echos in) so I put in a pause in the statement (as you can tell I'm not script writer). However I think it shows where the Left and Right numbering is failing.

C:\Users\stevem\Pictures\test\008_R>(
set /A LZL=2*1-1
set LZL=00!LZL!
set LZL=!LZL:~-3!
echo !LZL!
set /A LZR=2*1
set LZR=00!LZR!
set LZR=!LZR:~-3!
pause
magick !LZL!_L.jpg !LZR!_R.jpg +append output/page1.jpg
)
!LZL!
Press any key to continue . . .

Re: append left and right page images

Posted: 2017-11-29T10:28:01-07:00
by snibgo
Add a new line at the top of the BAT file:

Code: Select all

setlocal enabledelayedexpansion

Re: append left and right page images

Posted: 2017-11-29T10:45:31-07:00
by StevePM
GeeMack: a neat solution, as the left and right page sizes are equal. Just ran it and it created nicely paired left and right pages as single images.
Thank you

Re: append left and right page images

Posted: 2017-12-01T02:06:32-07:00
by StevePM
snibgo: your solution worked nicely as well, and allows for some further editing (coz i can understand what it is happening :) )