loop merge side by side jpgs in Cygwin

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
quanghai
Posts: 1
Joined: 2018-01-30T19:47:23-07:00
Authentication code: 1152

loop merge side by side jpgs in Cygwin

Post by quanghai » 2018-01-30T19:55:17-07:00

Dear All

I am new to both Cygwin and Imagemagic, but I am planning to convert to Linux soon,
thus I would like to work in Cygwin for the moment, so I can take the algorithm with
me.

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
etc.

Thank you

User avatar
GeeMack
Posts: 571
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: loop merge side by side jpgs in Cygwin

Post by GeeMack » 2018-01-31T12:17:29-07:00

quanghai wrote:
2018-01-30T19:55:17-07:00
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
etc.
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.

Post Reply