Page 1 of 1

Re: loop merge side by side jpgs in Cygwin

Posted: 2018-01-31T12:17:29-07:00
by GeeMack
quanghai wrote:
I have (say one) folder with jpg images (scans of books), and would like to merge them side by side, like a "dual reading mode), so

a1.jpg will be merged with a2.jpg
a3.jpg will be merged with a4.jpg
Every Linux, Cygwin, *nix flavor should have the tools "ls" and "sed". You can use "ls" piped through "sed" to create a couple of text files that will each have a list of every other JPG file, one with all the evens and the other with all the odds.

ImageMagick can then read those two text files as its input, pad the images to double their width, and locate the images on the right or left half as required with "-distort affine ...". Then it composites them together in left and right pairs to create the result of the second input image attached to the right of the first, the fourth input attached to the right side of the third, etc. A description of how that composition method works can be found at THIS link.

Here is a set of commands that should do what you're trying to accomplish...

Code: Select all

ls *.jpg | sed -n "1~2p" > list1.txt

ls *.jpg | sed -n "0~2p" > list2.txt

convert @list1.txt null: @list2.txt -set option:distort:viewport "%[fx:w*2]x%[h]" \
   -virtual-pixel none -distort affine "0,0 %[fx:(t+1)>(n/2)?w:0],0" -layers composite \
   -background white -alpha remove output%03d.jpg
A few things to note...

If there is an odd number of images, the last output will be a single image on the left half of the full sized page. The right half will be white.

This will work fairly fast if you have enough memory to read in all the files, but it will slow down dramatically if it has to swap to disk. In that case, it might be better to build a command loop and process the images as separate pairs.

If your input files are numbered like "img8.jpg", "img9.jpg", "img10.jpg", the "ls" of the directory may not list the files in order. The "img10.jpg" might come after the "img1.jpg", "img20.jpg" after "img2.jpg" and so forth. You may have to rename your files numbered with leading zeros to get around that.

If you want the output files to start numbering with "001" instead of "000" you can add "-scene 1" just before the output file name.