append left and right page images

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
StevePM
Posts: 6
Joined: 2017-11-29T05:30:49-07:00
Authentication code: 1152

append left and right page images

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: append left and right page images

Post 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.
snibgo's IM pages: im.snibgo.com
StevePM
Posts: 6
Joined: 2017-11-29T05:30:49-07:00
Authentication code: 1152

Re: append left and right page images

Post by StevePM »

hi i'm Running Windows 10, so I guess a .bat file
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: append left and right page images

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: append left and right page images

Post 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.
Last edited by GeeMack on 2017-11-29T10:09:02-07:00, edited 2 times in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: append left and right page images

Post 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".
snibgo's IM pages: im.snibgo.com
StevePM
Posts: 6
Joined: 2017-11-29T05:30:49-07:00
Authentication code: 1152

Re: append left and right page images

Post 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 . . .
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: append left and right page images

Post by snibgo »

Add a new line at the top of the BAT file:

Code: Select all

setlocal enabledelayedexpansion
snibgo's IM pages: im.snibgo.com
StevePM
Posts: 6
Joined: 2017-11-29T05:30:49-07:00
Authentication code: 1152

Re: append left and right page images

Post 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
StevePM
Posts: 6
Joined: 2017-11-29T05:30:49-07:00
Authentication code: 1152

Re: append left and right page images

Post by StevePM »

snibgo: your solution worked nicely as well, and allows for some further editing (coz i can understand what it is happening :) )
Post Reply